为什么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.
驱动程序的下拉列表。没有出现,只显示它是一个类...任何建议?
答案 0 :(得分:2)
您需要在@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.
答案 1 :(得分:1)
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()
应该使用相同的语法
答案 2 :(得分:0)
这里的问题是关于Sharfia和nullpointer提到的变量驱动程序范围的。在这里,我描述有关它的一些简历。
类范围变量:您希望能够从Java类中的任何位置访问的变量
公共类用户{ 私有字符串userName; }
方法范围变量:您可能想将它们设置为临时变量,最好只将它们用于一种方法。
公共无效总和(int a,int b){ int结果= a + b; }
循环作用域变量:在循环内部创建的变量是局部于顶部的吗?这意味着一旦退出循环,就无法再访问该变量
public void showNumbers(){ for(int i = 0; i <10:i ++){ System.out.println(“ count:” + i); } }
在这种情况下,您的驱动程序变量声明必须是一个类变量。
我使用此页面来获取示例和定义:https://www.java-made-easy.com/variable-scope.html