当我想扩展类并在打开登录函数 URL 的情况下调用 NewTest 类时遇到问题,但之后没有任何反应,测试不会继续登录,函数不会执行。
这是我从文件属性加载 URL 的类。
package base;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
public class BaseTest {
public static WebDriver driver;
public static Properties props = new Properties();
public static FileReader fr;
@BeforeTest
public static void main(String[] args) throws IOException {
if(driver == null) {
FileReader fr = new FileReader("C:\\Users\\stefa\\eclipse-workspace\\New folder\\selenium\\src\\test\\resources\\configfiles\\config.properties");
props.load(fr);
}
if(props.getProperty("browser").equalsIgnoreCase("chrome")) {
WebDriver driver = new ChromeDriver();
driver.get(props.getProperty("baseUrl"));
driver.manage().window().maximize();
}
}
}
这是我扩展上一课并继续使用登录表单的课程。
package testcase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import base.BaseTest;
public class NewTest extends BaseTest {
@Test
public static void loginTest(String[] args) {
WebElement userName = driver.findElement(By.id("user-name"));
userName.sendKeys("standard_user");
WebElement password = driver.findElement(By.id("password"));
password.sendKeys("secret_sauce");
WebElement loginBtn = driver.findElement(By.id("login-button"));
loginBtn.click();
}
}
我无法登录该页面。
您的代码示例存在多个问题。
main()
方法,注释为 @Test
方法。我认为 TestNG 不会发现您的 main()
方法作为套件级别的方法。您的@Test
方法也同样适用main()
作为配置方法并调用它,您的 driver
仍然为 null,因为您正在声明一个局部变量(也称为 driver
)并初始化 ChromeDriver
实例到它。因此,当 TestNG 退出该方法时, driver
实例超出范围,因此您的类级别静态数据成员永远不会被初始化(我相信这在 java 中称为阴影。请在下面找到代码的清理版本。 这里要注意的一点是,我使用
ThreadLocal
变量来设置和清除您的 WebDriver
实例,这样即使您决定并行运行 @Test
方法(在将来的某个时刻),您的代码仍然是线程安全的。
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import java.io.FileReader;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;
public class BaseTest {
private final ThreadLocal<WebDriver> driver = new ThreadLocal<>();
private static final Properties props = new Properties();
@BeforeSuite
public void readProperties() throws IOException {
FileReader fr = new FileReader("src/test/resources/configfiles/config.properties");
props.load(fr);
}
@BeforeMethod
public void setupDriver() {
if (props.getProperty("browser").equalsIgnoreCase("chrome")) {
WebDriver localDriver = new ChromeDriver();
localDriver.get(props.getProperty("baseUrl"));
localDriver.manage().window().maximize();
driver.set(localDriver);
}
}
@AfterMethod
public void closeDriver() {
if (driver.get() != null) {
driver.get().quit();
driver.remove();
}
}
public final WebDriver driver() {
return Objects.requireNonNull(driver.get(), "No valid driver instance found.");
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
public class NewTest extends BaseTest {
@Test
public void loginTest() {
WebElement userName = driver().findElement(By.id("user-name"));
userName.sendKeys("standard_user");
WebElement password = driver().findElement(By.id("password"));
password.sendKeys("secret_sauce");
WebElement loginBtn = driver().findElement(By.id("login-button"));
loginBtn.click();
}
}