为什么Selenium WebDriver
命令不会出现在我的@Test(priority=2)
上?每次我在@Test(priority=1)
测试中输入Selenium Web Driver命令时,它都不适用于其他测试使用 -
public class TestingTestNG {
@Test(priority=1)
public void TestingTestNG() throws InterruptedException{
// Import FireFox Driver
WebDriver driver = new FirefoxDriver();
// Open up Sand Page
driver.get("****");
// Enter Usename and Password
// User
driver.findElement(By.id("userId")).sendKeys("****");
Thread.sleep(3000);
// Password
driver.findElement(By.id("password")).sendKeys("****");
Thread.sleep(3000);
// Click Login Button
driver.findElement(By.id("loginButton")).click();
}
@Test(priority=2)
public void test2(){
driver.
驱动程序的下拉列表。没有出现,只显示它是一个类...任何建议?
您需要在@Test之外声明您的驱动程序。
public class TestingTestNG {
WebDriver driver = new FirefoxDriver();
@Test(priority=1)
public void TestingTestNG() throws InterruptedException{
// Open up Sand Page
driver.get("****");
// Enter Usename and Password
// User
driver.findElement(By.id("userId")).sendKeys("****");
Thread.sleep(3000);
// Password
driver.findElement(By.id("password")).sendKeys("****");
Thread.sleep(3000);
// Click Login Button
driver.findElement(By.id("loginButton")).click();
}
@Test(priority=2)
public void test2(){
driver. // here you will get your options.
driver
的范围不正确导致,尝试更改范围如下:
public class TestingTestNG {
// Import FireFox Driver
WebDriver driver = new FirefoxDriver();
@Test(priority=1)
public void TestingTestNG() throws InterruptedException{
// Open up Sand Page
driver.get("****");
....
}
@Test(priority=2)
public void test2(){
driver. //and now you shall get what you are expecting
}
编辑 -
driver.findElement(By.xpath("//element x-path")).click()
应该使用相同的语法
这里的问题就像Sharfia提到的那样,并且对变量驱动程序的范围进行了null指针。在这里,我描述了一个关于它的简历。
在这种情况下,您的驱动程序变量声明必须是类变量。
我使用此页面来获取示例和定义:https://www.java-made-easy.com/variable-scope.html