所以我理解流利和明確的等待,但我永遠無法得到隱含的陳述去工作。我設法不使用表達式來處理它們,但是我設計了一個相當簡單的測試,但它只適用於Thread.sleep,並且我絕對HATE該方法,並且不惜一切代價避免它。所以我試圖再次隱式等待函數...失敗。我的代碼Thread.sleep有效,但不是隱式等待?
的用了Thread.Sleep下面的代碼按預期工作,是偉大的
package myPackages;
import java.util.concurrent.TimeUnit;
// Unit test testing the Main User Drop Down Menu
// This tests the following:
// - changing status to Online, Away, Busy, Invisible via Left menu
// - Going to Settings
// - Logging out
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class statusTest {
private static WebDriver driver = new SafariDriver();
public static String HOME_URL = "http://localhost:3000";
public static String currentUserStatus;
private static By usernameOrEmailFieldLocator = By.id("emailOrUsername");
private static By passwordFieldLocator = By.id("pass");
private static By loginButtonLocator = By.cssSelector("button.button.primary.login");
private static By openMenuLocator = By.cssSelector("span.arrow.bottom");
private static By onlineButtonLocator = By.cssSelector("button.status.online");
private static By awayButtonLocator = By.cssSelector("button.status.away");
private static By busyButtonLocator = By.cssSelector("button.status.busy");
private static By invisibleButtonLocator = By.cssSelector("button.status.offline");
private static By userStatus = By.className("thumb");
@BeforeClass
public static void beforeClass() {
driver.get(HOME_URL);
driver.findElement(usernameOrEmailFieldLocator).sendKeys("adrian");
driver.findElement(passwordFieldLocator).sendKeys("adrian");
driver.findElement(loginButtonLocator).click();
}
@Before
public void before() throws Exception {
Thread.sleep(100);
new WebDriverWait(driver, 3).until(ExpectedConditions.presenceOfElementLocated(openMenuLocator)).click();
Thread.sleep(100);
new WebDriverWait(driver, 3).until(ExpectedConditions.presenceOfElementLocated(onlineButtonLocator)).click();
}
@AfterClass
public static void doEnd() {
driver.quit();
}
private static void changeStatusTo(By statusLocator) throws Exception {
Thread.sleep(100);
new WebDriverWait(driver, 3).until(ExpectedConditions.presenceOfElementLocated(statusLocator)).click();
Thread.sleep(100);
currentUserStatus = driver.findElement(userStatus).getAttribute("data-status");
}
@Test
public void setAway() throws Exception {
changeStatusTo(awayButtonLocator);
Assert.assertEquals("away", currentUserStatus);
}
@Test
public void setOnline() throws Exception {
changeStatusTo(onlineButtonLocator);
Assert.assertEquals("online", currentUserStatus);
}
@Test
public void setBusy() throws Exception {
changeStatusTo(busyButtonLocator);
Assert.assertEquals("busy", currentUserStatus);
}
@Test
public void setInvisible() throws Exception {
changeStatusTo(invisibleButtonLocator);
Assert.assertEquals("invisible", currentUserStatus);
}
}
但當我結束了試圖使用隱式(如下圖)這是行不通的。我確保在聲明驅動程序後立即聲明一次(本例中爲@beforeclass
)。在Before類中測試會失敗。我包括在最底部此頁面上的跟蹤堆棧:
package myPackages;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class statusTest {
private static WebDriver driver = new SafariDriver();
public static String HOME_URL = "http://localhost:3000";
public static String currentUserStatus;
private static By usernameOrEmailFieldLocator = By.id("emailOrUsername");
private static By passwordFieldLocator = By.id("pass");
private static By loginButtonLocator = By.cssSelector("button.button.primary.login");
private static By openMenuLocator = By.cssSelector("span.arrow.bottom");
private static By onlineButtonLocator = By.cssSelector("button.status.online");
private static By awayButtonLocator = By.cssSelector("button.status.away");
private static By busyButtonLocator = By.cssSelector("button.status.busy");
private static By invisibleButtonLocator = By.cssSelector("button.status.offline");
private static By userStatus = By.className("thumb");
@BeforeClass
public static void beforeClass(){
driver.manage().timeouts().implicitlyWait(100, TimeUnit.MILLISECONDS);
driver.get(HOME_URL);
driver.findElement(usernameOrEmailFieldLocator).sendKeys("adrian");
driver.findElement(passwordFieldLocator).sendKeys("adrian");
driver.findElement(loginButtonLocator).click();
}
@Before
public void before() throws Exception {
driver.findElement(openMenuLocator).click();
driver.findElement(onlineButtonLocator).click();
}
@AfterClass
public static void doEnd() {
driver.quit();
}
private static void changeStatusTo(By statusLocator) {
driver.findElement(statusLocator).click();
currentUserStatus = driver.findElement(userStatus).getAttribute("data-status");
}
@Test
public void setAway() {
changeStatusTo(awayButtonLocator);
Assert.assertEquals("away", currentUserStatus);
}
@Test
public void setOnline() {
changeStatusTo(onlineButtonLocator);
Assert.assertEquals("online", currentUserStatus);
}
@Test
public void setBusy() {
changeStatusTo(busyButtonLocator);
Assert.assertEquals("busy", currentUserStatus);
}
@Test
public void setInvisible() {
changeStatusTo(invisibleButtonLocator);
Assert.assertEquals("invisible", currentUserStatus);
}
}
org.openqa.selenium.NoSuchElementException:一個元素找不到使用給定的搜索參數的網頁上。 (警告:服務器未提供任何堆棧跟蹤信息) 命令持續時間或超時:999毫秒 有關此錯誤的文檔,請訪問:http://seleniumhq.org/exceptions/no_such_element.html 構建信息:版本:'unknown',修訂版本:'1969d75',時間:' 2016-10-18 09:43:45 -0700' 系統信息:host:'Adrians-iMac.local',ip:'10 .0.2.15',os.name:'Mac OS X',os.arch:' x86_64',os.version:'10 .12.1',java.version:'1.8.0_111' 驅動程序信息:org.openqa.selenium.safari.SafariDriver 功能[{applicationCacheEnabled = true,rotated = false,databaseEnabled = true ,handlesAlerts = true,version = 12602.2.14.0.5,cleanSession = true,platform = MAC,nativeEvents = true,locationContextEnabled = false,webStorageEnabled = true,browserName = safari,javascriptEnabled = true,cssSelectorsEnabled = true}] 會話ID:DADE0351-039B-4C06-BC65-05FB90E08202 ***元素信息:{Using = css selector,value = span.arrow.bottom} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun .reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在java.lang.reflect.Constructor.newInstance(Constructor.java:423) 在有機.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216) 在org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168) 在org.openqa.selenium.remote.RemoteWebDriver.execute (RemoteWebDriver.java:635) 在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368) 在org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:465) 在org.openqa.selenium.By $ ByCssSelector .findElement(By.java:430) 在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360) 在myPackages.statusTest.before(statusTest.java:53) 在sun.reflect.NativeMethodAccessorImpl .invoke0(本機方法) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke( Method.java:498) at org.junit.runners.model.Framewo rkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java :47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores。的java:24) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在org.junit.runners.BlockJUnit4ClassRunner。 rungild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71) at org。 junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58) at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java: 268) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.juni t.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner。 JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner。 runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run( (RemoteTestRunner.java:192)
據我所知,如果我使用3秒的隱式WebDriverWait爲10秒,那麼它會運行約13秒,取決於WebDriverWait的正確性?所以無論如何我改變了我的代碼是3秒,它似乎仍然沒有工作。相同的跟蹤堆棧 – Potion
您不需要使用隱式等待和顯式等待。此外,隱式和顯式等待不會加起來!一旦聲明瞭隱式等待,瀏覽器將在拋出異常之前等待x秒。 – prithvi394
所以我甚至不應該隱式等待,只使用我的原始代碼? – Potion