【问题标题】:Unable to run TestNG test case in Eclipse with ChromeDriver无法使用 ChromeDriver 在 Eclipse 中运行 TestNG 测试用例
【发布时间】:2016-09-16 23:28:23
【问题描述】:

我编写了下面的代码来在 chrome 浏览器中打开一个站点并验证它的标题。但是当使用System.setProperty() 设置ChromeDriver 路径时,它给了我语法错误,当我评论我得到的行时:

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property..

我的代码:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.Assert;
    import org.testng.annotations.Test;

   public class FirsttestNGFile {
    String BaseURL = "http://newtours.demoaut.com/";
    System.setProperty("webdriver.chrome.driver", "E:\\Automation Jars\\chromedriver_win32\\chromedriver.exe"); -- If I comment this line, I get Illegal state Exception for chromedriver path; if not commented , I get syntax error
    WebDriver driver = new ChromeDriver();  

    @Test
    public void verifyHomePageTitle() {     
    driver.get(BaseURL);
    String ExpectedTitle = "Welcome: Mercury Tours";
    String ActualTitle = driver.getTitle();
    Assert.assertEquals(ExpectedTitle, ActualTitle);
    driver.quit();          
    }
  }  

【问题讨论】:

  • 我也试过Project->Clean;但这也无济于事

标签: java eclipse selenium testng


【解决方案1】:

您不能全局定义System.setProperty。 使用以下代码并尝试:

WebDriver driver;
@Before
public void browser(){
 System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\CP-SAT\\Chromedriver\\chromedriver.exe"); 
 driver = new ChromeDriver(); 
}

@Test
public void verifyHomePageTitle() {     

    String BaseURL = "http://newtours.demoaut.com/";
    driver.get(BaseURL);
    String ExpectedTitle = "Welcome: Mercury Tours";
    String ActualTitle = driver.getTitle();
    Assert.assertEquals(ExpectedTitle, ActualTitle);
    }

@Test
public void a() {
    driver.get("https://www.google.co.in/?gfe_rd=cr&ei=6PDbV-qTAZHT8gecr4qQBA");    
} 

@After
public void close(){
    driver.quit();
    }
}

如果您使用的是Junit,则使用@Before,或者如果您使用的是TestNG,则使用@BeforeTest

回复我以获取更多信息。 快乐学习。 :-)

【讨论】:

  • 注意:@Before 的 TestNG 等价物是 @BeforeMethod,而不是在一组类之前调用​​的 @BeforeTest@BeforeSuite-> @BeforeTest -> @BeforeClass -> @BeforeMethod
  • 我现在正在尝试运行多个测试用例,但是由于我在 @beforeTest 中声明了 Chromedriver,所以每次测试中使用的驱动程序都会出现编译错误,当我重新声明驱动程序时,它会打开每次都有一个新的 chrome 窗口。我希望我只声明一次驱动程序,并且每次都可以在我的测试方法中使用
【解决方案2】:

您应该考虑使用https://github.com/bonigarcia/webdrivermanager,这将为您完成这项工作:

ChromeDriverManager.getInstance().setup();

【讨论】:

    【解决方案3】:

    在环境变量路径中更新 chrome 驱动程序路径,然后尝试在脚本中使用以下代码

    @BeforeClass
    public void setup() {
        WebDriver driver = new ChromeDriver();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-14
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 2018-09-29
      • 1970-01-01
      相关资源
      最近更新 更多