2016-12-21 55 views
1

所以我注意到,在許多情況下,硒是如此之快,它無法捕捉在Web應用程序上滑動菜單的轉換。下面的例子不會運行,除非我把睡眠線程(我絕對討厭在我的代碼中)......這是唯一的方法嗎?這裏是變量設置和檢查元素是否存在的方法,實際測試在下一組代碼中。硒新等待,直到條件不工作強制使用Thread.Sleep

private static By awayButtonLocator = By.cssSelector("button.status.away"); 
    private static By awayStatusIconLocator = By.cssSelector("div.info.status-away"); 

    private static boolean isElementPresent(By targetElement){ 
     Boolean isPresent = driver.findElements(targetElement).size() > 0; 
     if(isPresent){ 
      return true; 
     }else{ 
      return false; 
     } 

    } 

因此,根據預期澄清工作以下的@test。然而thread.sleep必須在那裏,儘管我不想要10秒的WebDriverWait。我甚至嘗試了預期的情況elementToBeClickable,但那不起作用,仍然需要調用睡眠。所以在這種情況下,我必須有意地減慢代碼,以便使用線程睡眠捕捉元素?

@Test 
    public void setAway() throws Exception { 
     Thread.sleep(500); 
     new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(awayButtonLocator)).click(); 
     Assert.assertTrue(isElementPresent(awayStatusIconLocator)); 
    } 

跟蹤輸出:

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: 17 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: FE763803-D4F9-485F-A176-847A5E9911CA 
*** Element info: {Using=css selector, value=div.flex-nav.section.header.div.h4} 
    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.menuSuite.myAccountTest.checkMenuExist(myAccountTest.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.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
    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) 
+0

嘗試用'visibilityOf'。有許多ExpectedConditions可用。請評估並選擇最適合您的要求的最佳https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html –

+0

@Naveen我剛剛嘗試過,不幸的是它不工作。還有其他建議嗎? – Potion

+0

拋出的異常是什麼? TimeoutException異常?請添加堆棧跟蹤。 –

回答

0

我建議你同時在@Before鉤爲您的驅動程序implicitlyWait超時某處:

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);