硒新手
我有一个基类,它将启动webdriver
。以下页面可用于创建合同。
登录->选择计划->添加场所->添加详细信息
public class BaseClass {
public WebDriver driver;
public WebDriverWait wait;
public WebDriver getDriver() {
return driver;
}
@BeforeClass(description = "Class Level Setup!")
public void setup() {
// Create a Chrome driver. All test classes use this.
System.setProperty("webdriver.chrome.driver", "/Selenium/chromedriver");
driver = new ChromeDriver();
// Create a wait. All test classes use this.
wait = new WebDriverWait(driver, 15);
// Maximize Window
driver.manage().window().maximize();
}
}
然后登录。测试
public class LoginTest extends BaseClass {
@Test(priority = 0, description="This TC is for Senoko login")
void validateLogin() {
driver.get("https://url domain/index");
WebElement email = driver.findElement(By.id("userid_txt"));
WebElement password = driver.findElement(By.id("password_txt"));
WebElement login = driver.findElement(By.id("submitBtn"));
email.sendKeys("a01");
password.sendKeys("test123");
login.click();
HouseholdCheck house=new HouseholdCheck();
house.houseCheck();
}
}
到此为止,一切正常。在validateLogin()
方法中,调用houseCheck
方法,在这里我得到NullPointerException
。
public class HouseholdCheck extends BaseClass {
@Test
void houseCheck() {
System.out.println("=====" + driver);
driver.get("https://a-sit.aspx?source=CSP&key=1");
WebElement radio1 = driver.findElement(By.xpath("//input[@name='resbiz' and @value='households']"));
WebElement radio2 = driver.findElement(By.xpath("//input[@name='resbiz' and @value='business']"));
radio1.click();
WebElement radbutton = driver.findElement(By.xpath("//button[contains(.,'Continue')]"));
radbutton.click();
PlanSelect plnslct = new PlanSelect();
plnslct.planSelect(driver);
}
}
在HouseholdCheck
类中,无法访问driver
。提供NullPointerException
。请指导我,如何将驱动程序传递给后续课程?如果某个类扩展了BaseClass
,为什么该类无法获得驱动程序对象?
答案 0 :(得分:5)
在HouseholdCheck house = new HouseholdCheck();
中创建BaseClass
的新实例,其中从未初始化driver
。您将houseCheck()
作为方法调用,而不将其作为测试运行,因此setup()
未被执行。
一种解决方案是将driver
设为静态
private static WebDriver driver;
public static WebDriver getDriver() {
return driver;
}
答案 1 :(得分:1)
public class BaseClass {
public static WebDriver driver;
只需将此设置为静态,就可以了。