【问题标题】:Running multiple classes in TestNG在 TestNG 中运行多个类
【发布时间】:2017-09-14 10:25:05
【问题描述】:

我正在尝试自动化一个场景,其中我想登录一次应用程序,然后进行操作而无需再次重新登录。

考虑一下,我在特定类的@BeforeSuite 方法中有登录应用程序的代码。

public class TestNGClass1 {

    public static WebDriver driver;

    @BeforeSuite
    public static void setUp(){
        System.setProperty("webdriver.chrome.driver", "D://Softwares//chromedriver.exe");
        driver = new ChromeDriver(); 
        //driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.myfitnesspal.com");
    }

    @AfterSuite
    public static void close(){
        driver.close();
    }
}

我在 TestNGClass2 中有我的 @test 方法,它基本上试图点击一些登录按钮。

public class TestNGClass2 extends TestNGClass1 {

    public static WebDriver driver;

    @Test
    public static void login(){
        System.out.println("Entering the searchQuery Box");
        WebElement signUpWithEmailBtn = driver.findElement(By.xpath(".//*[@id='join']/a[2]"));
        System.out.println("srchTxtBox Box");
        signUpWithEmailBtn.click();
    }   
}

我有另一个类 TestNGClass3,它有另一个 @Test 方法,需要在 TestNGClass2 完成后运行。

public class TestNGClass3 extends TestNGClass1{

public static WebDriver driver;

    @Test
    public static void signIn(){
        WebElement emailAddress = driver.findElement(By.id("user_email"));
        emailAddress.clear();
        emailAddress.sendKeys("asdsa@gmail.com");
        WebElement password = driver.findElement(By.id("user_password"));
        password.clear();
        password.sendKeys("sdass");

        WebElement continueBtn = driver.findElement(By.id("submit"));
        continueBtn.click();
    }
}

testng.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="Regression Test">
        <classes>
            <class name="com.test.TestNGClass2" />
            <class name="com.test.TestNGClass3" />
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

我的方法是否正确,因为当代码到达 TestNGClass2 的“登录”方法时出现“空指针”异常?

【问题讨论】:

    标签: java selenium testng


    【解决方案1】:

    我认为你只需要在 TestNGClass2TestNGClass3 中删除这条线

    public static WebDriver driver;
    

    您已经将driver 存储在基类TestNGClass1 中,因此当您在其他类中包含该行时,您基本上隐藏了被实例化的行。

    我还考虑将基类访问修饰符更改为protected,因为您可能不希望不是该基类的子类访问driver

    【讨论】:

    • 是的可能!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    相关资源
    最近更新 更多