TestNG并行注释​​:测试运行正常,但仍然得到Element不可见的异常

时间:2015-03-14 17:44:17

标签: java selenium selenium-webdriver

我正在尝试运行以下代码。脚本运行并且浏览器窗口打开并运行但我得到ElementNotvisibleException。 我试图在两个不同的浏览器中并行运行它。

" Selenium"得到键入,但我仍然得到错误。

package TestNG1;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;

public class Browser {

  public WebDriver driver;
  @BeforeMethod
  @Parameters({"browser"})
  public void beforeMethod(String browser) {
      if (browser.equalsIgnoreCase("firefox")){
          System.out.println("True Fox");
          driver = new FirefoxDriver();
      }
      else
      {
          System.setProperty("webdriver.chrome.driver","//home//sonam//Desktop//chromedriver");
          System.out.println("True chrome");
          driver = new ChromeDriver();
      }

  }

  @AfterMethod
  public void afterMethod() {
      driver.close();
  }

}

Class Browser2:

package TestNG1;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class Browser2 extends Browser{
  @Test
  public void f() throws InterruptedException {

      driver.get("https://www.google.com");
      String t1 = driver.getTitle();
      System.out.println("title1:"+t1);
      WebDriverWait mywait = new WebDriverWait(driver,10);
      mywait.until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
      driver.findElement(By.name("q")).sendKeys("Selenium");
       mywait.until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
      driver.findElement(By.name("btnK")).click();  

  }
}

Browser2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="tests">
  <test name="Firefox_Test">
  <parameter name= "browser" value="FIREFOX" />
    <classes>
      <class name="TestNG1.Browser2"/>
    </classes>
  </test> 
  <test name="Chrome_Test">
  <parameter name= "browser" value="CHROME" />
    <classes>
      <class name="TestNG1.Browser2"/>
    </classes>
  </test>
</suite> <!-- Suite -->

我在Ubuntu中运行它。

错误:

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 69 milliseconds
Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35'
System info: host: 'sonam-SVE14123CNB', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-34-generic', java.version: '1.7.0_65'
Session ID: 0aa9f525-4bbf-4219-8816-da40f8f213e7
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=36.0}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79)
    at TestNG1.Browser2.f(Browser2.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:37)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
    at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Build info: version: '2.45.0', revision: '32a636c', time: '2015-03-05 22:01:35'
System info: host: 'sonam-SVE14123CNB', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-34-generic', java.version: '1.7.0_65'
Driver info: driver.version: unknown
    at <anonymous class>.fxdriver.preconditions.visible(file:///tmp/anonymous9045443040518399534webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:9587)
    at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///tmp/anonymous9045443040518399534webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12257)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///tmp/anonymous9045443040518399534webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///tmp/anonymous9045443040518399534webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///tmp/anonymous9045443040518399534webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)

任何帮助都会有用。

1 个答案:

答案 0 :(得分:0)

您可能打算等待第二次等待中btnK的可见性。

替换第二个mywait.until()电话:

mywait.until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));

使用:

mywait.until(ExpectedConditions.visibilityOfElementLocated(By.name("btnK")));