Selenium Web驱动程序和TESTNG

问题描述 投票:0回答:3

为什么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.

驱动程序的下拉列表。没有出现,只显示它是一个类...任何建议?

selenium selenium-webdriver testng qa
3个回答
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
投票

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()

应该使用相同的语法


0
投票

这里的问题就像Sharfia提到的那样,并且对变量驱动程序的范围进行了null指针。在这里,我描述了一个关于它的简历。

  • 类范围变量:您希望能够从Java类中的任何位置访问的变量 public class User {private String userName; }
  • 方法范围变量:您可能想要临时创建的一些变量,最好只用于一个方法。 public void sum(int a,int b){int result = 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

© www.soinside.com 2019 - 2024. All rights reserved.