一共有三个班级。一类用于浏览器初始化(BrowserConfig.java),一类用于存储页面的元素(LoginPageElements.java),一类用于执行测试用例(Login_positive_case.java)。
BrowserConfig.java
package com.midcities.utility;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.paulhammant.ngwebdriver.NgWebDriver;
public class BrowserConfig {
public static WebDriver startApplication(WebDriver driver, String browsername, String AppUrl) {
if(browsername.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");
driver = new ChromeDriver();
} else if(browsername.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.gecko.driver", "./Drivers/geckodriver.exe");
driver = new FirefoxDriver();
} else {
System.out.println("Browser not supported");
}
NgWebDriver ngWebDriver = new NgWebDriver((JavascriptExecutor) driver);
ngWebDriver.waitForAngularRequestsToFinish();
driver.manage().window().maximize();
driver.get(AppUrl);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
return driver;
}
public static void quitBrowser(WebDriver driver) {
driver.quit();
}
}
LoginPageElements.java
package com.midcities.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import com.paulhammant.ngwebdriver.ByAngular;
public class LoginPageElements {
WebDriver driver;
public LoginPageElements(WebDriver driver){
this.driver = driver;
}
public void Login(String uname, String pass) {
driver.findElement(By.id("mat-input-0")).sendKeys(uname);
driver.findElement(By.id("mat-input-1")).sendKeys(pass);
driver.findElement(ByAngular.buttonText("SIGN IN")).click();
}
}
Login_positive_case.java
package com.midcities.testcases;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import com.midcities.pages.LoginPageElements;
import com.midcities.utility.BrowserConfig;
public class Login_Positive_Case {
WebDriver driver;
@Test
public void loginIntoTheApplication() {
BrowserConfig.startApplication(driver, "chrome", "http://test.com/");
LoginPageElements loginFunc = new LoginPageElements(driver);
loginFunc.Login("[email protected]", "test");
}
}
控制台日志
FAILED: loginIntoTheApplication
java.lang.NullPointerException
at com.midcities.pages.LoginPageElements.Login(LoginPageElements.java:20)
at com.midcities.testcases.Login_Positive_Case.loginIntoTheApplication(Login_Positive_Case.java:20)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
最佳答案
BrowserConfig.startApplication
在方法范围内初始化新的WebDriver
,您需要使用返回的值
driver = BrowserConfig.startApplication(driver, "chrome", "http://test.com/");