这些是不同包装中的各种代码。
这是我的AutoConst界面:
package generic;
public interface AutoConst
{
String Gecko_Key="webdriver.gecko.driver";
String Gecko_Value="./software/geckodriver.exe";
String Chrome_Key="webdriver.chrome.driver";
String Chrome_Value="./software/chromedriver.exe";
String IE_Key="webdriver.ie.driver";
String IE_value="./software/IEDriverServer.exe";
}
这是我的BaseTest抽象类:
package generic;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
public abstract class BaseTest implements AutoConst
{
public WebDriver driver;
public void BrowserOpening(String url)
{
System.setProperty(Chrome_Key,Chrome_Value);
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(url);
}
public void BrowserClosing()
{
driver.quit();
}
}
这是我的BasePage Abstract类:
package pom.aut;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.Reporter;
public abstract class BasePage
{
public WebDriver driver;
public BasePage(WebDriver driver)
{
this.driver=driver;
PageFactory.initElements(driver, this);
}
//this method is used for verifying the title of the page
public void verifyTitle(String eTitle)
{
WebDriverWait Wait=new WebDriverWait(driver,10);
try
{
Wait.until(ExpectedConditions.titleContains(eTitle));
Reporter.log("Title is matching");
}
catch(Exception e)
{
Reporter.log("title is not matching");
Assert.fail();
}
}
//this method is used to verify Element is present or not
public void verifyElementIsPresent(WebElement element)
{
WebDriverWait Wait=new WebDriverWait(driver,10);
try
{
Wait.until(ExpectedConditions.visibilityOf(element));
Reporter.log("Element is present");
}
catch(Exception e)
{
Reporter.log("Element is not present");
Assert.fail();
}
}
}
这是我的Pom课程:
package pom.aut;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class LoginPage
{
WebDriver driver;
public LoginPage(WebDriver ldriver)
{
this.driver=ldriver;
}
@FindBy(xpath="//input[@id='Email']")
private WebElement emailTB ;
@FindBy(xpath="//input[@id='Password']")
private WebElement passwordTB;
@FindBy(xpath="//button[contains(text(),'Log')]")
private WebElement loginBTN;
public void LoginAccount(String email,String password)
{
emailTB.sendKeys(email);
passwordTB.sendKeys(password);
loginBTN.click();
}
}
这是我的考试班: 打包测试脚本;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
import generic.BaseTest;
import pom.aut.LoginPage;
public class Login_tc003 extends BaseTest
{
@Test
public void testValidLoginPage()
{
Login_tc003 login=new Login_tc003();
login.BrowserOpening("https://dev.autkit.com/Account/Login");
LoginPage l=new LoginPage(driver);
l.LoginAccount("tomharry001@gmail.com", "tomharry1");
}
}
控制台错误::::::::::::::::
[RemoteTestNG] detected TestNG version 6.14.2
Starting ChromeDriver 2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a)
on port 19807
Only local connections are allowed.
Nov 19, 2018 4:58:23 PM org.openqa.selenium.remote.ProtocolHandshake
createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the
remote end``
Nov 19, 2018 4:58:46 PM org.openqa.selenium.remote.ProtocolHandshake
createSession
INFO: Detected dialect: OSS
FAILED: testValidLoginPage
java.lang.NullPointerException
at pom.aut.LoginPage.LoginAccount(LoginPage.java:25)
at testscript.Login_tc003.testValidLoginPage(Login_tc003.java:27)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown
Source)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHe
lper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.ja
va:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
答案 0 :(得分:0)
在您的POM类中创建一个构造函数。
public LoginPage(){
PageFactory.initelements(driver,this);
}
希望在此之后可以尝试。
答案 1 :(得分:0)
您的问题是尝试将按钮存储为WebElement,然后单击它。
直接执行driver.find .... click()或使用操作。
Actions actions = new Actions(a);
actions.moveToElement(loginBTN);
actions.click();