在 TestNG 中运行多个类 [英] Running multiple classes in TestNG

查看:53
本文介绍了在 TestNG 中运行多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to automate a scenario wherein, I want to Login once into the application & then do manipulations without having to re-login again.

考虑到这一点,我在特定类的@BeforeSuite 方法中有用于登录应用程序的代码.

Consider that, I have the code to login into the application in the @BeforeSuite method in a specific class.

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 方法,它基本上试图点击一些登录按钮.

I have my @test method in TestNGClass2 which basically tries to click on some login buttons.

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 完成后运行.

I have another class TestNGClass3 which has another @Test method which needs to be run after TestNGClass2 is completed.

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 文件如下:

testng.xml file is as follows:

<?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 的登录"方法时出现空指针"异常?

Is my approach right, since I'm getting "Null Pointer" exception when the code reaches the 'login' method of TestNGClass2 ?

推荐答案

我认为你只需要在 TestNGClass2TestNGClass3 中去掉这行>

I think you just need to get rid of this line in both your TestNGClass2 and TestNGClass3

public static WebDriver driver;

您已经将 driver 存储在基类 TestNGClass1 中,所以当您在其他类中有该行时,您基本上是隐藏了实例化.

You're already storing the driver in the base class TestNGClass1, so when you have that line in your other classes, you're basically hiding the one that is instantiated.

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

I'd also consider changing the base class access modifier to protected since you probably don't want classes that aren't children of that base class to access the driver.

这篇关于在 TestNG 中运行多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆