gpt4 book ai didi

java - 在 TestNG 中运行多个类

转载 作者:行者123 更新时间:2023-11-30 06:49:00 25 4
gpt4 key购买 nike

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

考虑一下,我在特定类的@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 的“登录”方法时出现“空指针”异常?

最佳答案

我认为您只需要在 TestNGClass2TestNGClass3 中删除这一行

public static WebDriver driver;

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

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

关于java - 在 TestNG 中运行多个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43474568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com