首页 文章

我的代码Thread.sleep工作但不是隐式等待?

提问于
浏览
1

所以我理解流利和明确的等待,但我永远不会让隐含的陈述在过去起作用 . 我设法不使用explicits处理它们,但我设计了一个相当简单的测试,但它只适用于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构建信息:版本:'未知',修订版:'1969d75',时间:'2016-10-18 09:43:45 -0700'系统信息:主持人:'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,可旋转= 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.ref位于org.openqa.selenium的java.lang.reflect.Constructor.newInstance(Constructor.java:423)的sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)中的lect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) .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(母语方法)at sun.reflect.NativeMethodAccessorImpl.invoke(N ativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.Method.invoke(Method.java:498)在1 org.junit.runners.model.FrameworkMethod $ . org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)中的orRe.Rerners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)中的runReflectiveCall(FrameworkMethod.java:50) .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.runChild(BlockJUnit4ClassRunner.java:57)org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner $ 1.schedule( ParentRunner.java:71)org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)org.junit.runners.ParentRunner.access $ 000(Paren tRunner.java:58)org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)org.junit.runners.statements.RunBefores.evaluate(RunBefores.java:26)org.junit . org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run上的org.junit.runners.ParentRunner.run(ParentRunner.java:363)中的internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java: 459)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)在有机.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

1 回答

  • 1

    在隐式等待而不是毫秒中使用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秒

    如果timeunit HAS以毫秒为单位,则应该在隐式等待中使用3000ms .

相关问题