简体   繁体   English

在TestNG中运行多个类

[英]Running multiple classes in 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. 我正在尝试自动化一个场景,在该场景中,我想一次登录到应用程序中,然后进行操作而不必再次重新登录。

Consider that, I have the code to login into the application in the @BeforeSuite method in a specific class. 考虑到这一点,我有代码可以通过特定类中的@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();
    }
}

I have my @test method in TestNGClass2 which basically tries to click on some login buttons. 我在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();
    }   
}

I have another class TestNGClass3 which has another @Test method which needs to be run after TestNGClass2 is completed. 我有另一个类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 file is as follows: 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 -->

Is my approach right, since I'm getting "Null Pointer" exception when the code reaches the 'login' method of TestNGClass2 ? 我的方法正确吗,因为当代码到达TestNGClass2的“ login”方法时,我得到了“ Null Pointer”异常?

I think you just need to get rid of this line in both your TestNGClass2 and TestNGClass3 我认为您只需要在TestNGClass2TestNGClass3中都摆脱这一行

public static WebDriver driver;

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. 您已经将driver存储在基类TestNGClass1 ,因此,在其他类中包含该行时,基本上就隐藏了实例化的那一行。

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 . 我还考虑将基类访问修饰符更改为protected因为您可能不希望不是该基类的子类的类访问driver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM