- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我理解流畅和显式的等待,但我过去永远无法让隐式语句发挥作用。我设法不使用显式处理它们,但随后我设计了一个相当简单的测试,但它只能与 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: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 999 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700' System info: 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' Driver info: org.openqa.selenium.safari.SafariDriver Capabilities [{applicationCacheEnabled=true, rotatable=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}] Session ID: DADE0351-039B-4C06-BC65-05FB90E08202 *** Element info: {Using=css selector, value=span.arrow.bottom} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368) at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:465) at org.openqa.selenium.By$ByCssSelector.findElement(By.java:430) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360) at myPackages.statusTest.before(statusTest.java:53) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$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) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(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.junit.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:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
最佳答案
在隐式等待中使用 TimeUnit.SECONDS 而不是毫秒。快速浏览一下您的代码表明您在隐式等待中使用了 100 毫秒,与在显式等待中分配的 3 秒相比,这只是名义上的。
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//waits for 3 seconds
注意:显式等待的单位是秒。
new WebDriverWait(driver, 10) //will wait for 10 seconds
1000毫秒=1秒
如果时间单位必须以毫秒为单位,则应在隐式等待中使用 3000 毫秒。
关于java - 我的代码 Thread.sleep 有效,但不能隐式等待?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41348805/
完成此任务的最佳方法是什么:主线程(线程 A)创建另外两个线程(线程 B 和线程 C)。线程 B 和 C 执行繁重的磁盘 I/O,最终需要将它们创建的资源传递给线程 A,然后调用外部 DLL 文件中的
我是一名对 Julia 语言感兴趣的新手程序员。文档( https://docs.julialang.org/en/v1/base/multi-threading/ )说 Threads.@threa
python中的thread.start_new_thread和threading.Thread.start有什么区别? 我注意到,当调用 start_new_thread 时,新线程会在调用线程终止
我正在学习安卓蓝牙编程。我从 Google 的 Android 开发者网站上复制了大部分代码以供学习。这个想法是监听服务器上的连接是在一个新线程中完成的,而不会阻塞 UI 线程。当收到连接请求时,连接
执行对象方法的线程是否依赖于创建它的线程上的线程? 假设您的 java 应用程序中有两个线程 Thread1 和 Thread2,以及两个类 ClassA 和 ClassB。 您在 Thread1 上
我正在用这段代码测试 C++11 线程,但是在创建线程时,我遇到了错误没有匹配函数调用 'std::thread::thread()'. 这就像我给 std::thread ctr 的函数有什么问题,
我有如下类 eventEngine 和网关: class eventEngine { public: eventEngine(); std::thread threa; std
我需要运行与列表 dirlist 中的元素一样多的 Observer 类线程。当我运行它 python 控制台时,它可以正常工作。 class Observer(Thread): def ru
我在一本 Java 书中读到了下面的代码。我知道主类默认继承 Thread 类,所以 currentThread();而不是 Thread.currentThread();也会做这项工作。 但我不明白
我在我的系统中使用第 3 方 API,该 API 启动一个永久运行的用户线程。一旦我的程序结束,JVM 由于该线程而继续运行,因此我尝试获取此线程引用并通过更改它 thread.setDaemon(t
我所知道的 Python 中的每个对象都可以通过调用来处理其基类初始化: super(BaseClass, self).__init__() threading.Thread 的子类似乎不是这种情况,
在我最近从事的 Xamarin 项目中,我可以看到开发人员使用了 Java.Lang.Thread 以及 System.Threading.Thread(用于非常相似的操作 - 例如在后台加载数据)。
我在 Julia 中运行双循环。代码非常简单。 w = rand(1000,1000) function regular_demo(w::Array{Float64, 2}) n = size
我在 Windows 上使用 Python 3。我正在使用 threading.Thread动态运行一个函数,我可以带参数或不带参数调用它。我正在设置一个列表,其中的第一项是定义路径的字符串。其他参数
我遇到了一些使用线程模块(使用 Python 2.6)管理线程的示例。 我想了解的是这个例子是如何调用“运行”方法的,在哪里调用的。我在任何地方都看不到它。 ThreadUrl 类在 main() 函
假设我从 threading.Thread 派生: from threading import Thread class Worker(Thread): def start(self):
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
使用 WinDbg 和 SOS,我有以下内容: 0:011> !threads ThreadCount: 7 UnstartedThread: 0 BackgroundThread: 4 Pendin
App Engine 给出错误: com.google.apphosting.api.ApiProxy$CallNotFoundException: Can't make API call urlfe
我正在尝试将 Swing JEditorPane 嵌入到 JavaFX 项目中,如下代码所示。 Platform.runLater(() -> { SyntaxTester ob = new
我是一名优秀的程序员,十分优秀!