对2个不同的testNg类进行了4页和2次测试。 当我运行testNG xml时,它们抛出一个空指针异常,我真的不知道为什么。 您能否告诉我如何修改以下代码以使其工作并解释原因?
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class DefaultLandingPage {
private WebDriver driver;
public DefaultLandingPage(WebDriver driver){
this.driver = driver;
}
//Find Login link by using xpath
@FindBy (xpath = "//*[@id='navbar-login-link']")
private WebElement loginLink;
//Clicking login link
public void clickLoginButton(){
loginLink.click();
}
}
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class LoginPage {
private WebDriver driver;
public LoginPage(WebDriver driver){
this.driver=driver;
}
//Using FindBy for locating elements
@FindBy (id="user_login")
private WebElement userName;
@FindBy (id="user_pass")
private WebElement password;
@FindBy (id="wp-submit")
private WebElement submitButton;
@FindBy (id="rememberme")
private WebElement rememberMeCheckBox;
public void login_wordpress(String uid,String pass)
{
userName.sendKeys(uid);
password.sendKeys(pass);
submitButton.click();
}
public void doNotRememberMe (){
if (rememberMeCheckBox.isSelected())
rememberMeCheckBox.click();
}
}
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class FollowingReader {
private WebDriver driver;
public String expectedPageTitle = "Following > Reader - WordPress.com";
public FollowingReader(WebDriver driver){
this.driver=driver;
}
@FindBy (how=How.XPATH,using="//*[@title='Create a New Post']")
private WebElement createNewPostButton;
public void createNewPost(){
createNewPostButton.click();
}
public String actualPageTitle(){
driver.getTitle();
return actualPageTitle();
}
}
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class PostPage {
private WebDriver driver;
public PostPage(WebDriver driver){
this.driver=driver;
}
@FindBy(how=How.XPATH,using="//*[@id='primary']/div/div[2]/div[1]/div[3]/div/button[1]")
private WebElement publishButton;
@FindBy(how=How.XPATH,using="//*[@id='primary']/div/div[2]/div[2]/div[1]/div[5]/div/textarea")
private WebElement postTextField;
public void writeNewPost(String text){
postTextField.sendKeys(text);
publishButton.click();
}
}
package tests;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import pages.DefaultLandingPage;
import pages.LoginPage;
public class Login {
WebDriver driver = new FirefoxDriver();
@BeforeSuite
public void LaunchMozilla() {
System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver-v0.16.0-win64\\geckodriver.exe");
driver.get("http:\\wordpress.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@Test(description = "Login with valid user", priority =1)
public void loginValidUser() throws InterruptedException{
DefaultLandingPage landing_page =PageFactory.initElements(driver, DefaultLandingPage.class);
landing_page.clickLoginButton();
LoginPage login_page=PageFactory.initElements(driver, LoginPage.class);
login_page.doNotRememberMe();
login_page.login_wordpress("user", "pw");
}
}
package tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.Test;
import pages.FollowingReader;
import pages.PostPage;
public class WriteNewPost {
WebDriver driver;
@Test(description = "Check window title", priority = 1)
public void checkWindowTitle() {
FollowingReader followingReaderPage=PageFactory.initElements(driver, FollowingReader.class);
Assert.assertEquals(followingReaderPage.expectedPageTitle, followingReaderPage.actualPageTitle());
}
@Test (description = "Click on create new post button", priority = 2)
public void clickOnCreateNewPost(){
FollowingReader followingReaderPage=PageFactory.initElements(driver, FollowingReader.class);
followingReaderPage.createNewPost();
}
@Test (description = "Write a new post and post it", priority = 3)
public void createNewPost(){
PostPage post_page = PageFactory.initElements(driver, PostPage.class);
post_page.writeNewPost("This is a test");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite guice-stage="DEVELOPMENT" name="Default suite">
<test verbose="2" name="Default test">
<classes>
<class name="tests.Login"/>
<class name="tests.WriteNewPost"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
答案 0 :(得分:0)
我终于通过创建一个单独的java类来设法找到解决方案,该类通过创建单例来处理浏览器