我有两个测试,我试图通过TestNG xml运行。
如果我独立运行它,它就有效。但是,如果我将它作为带有xml的套件运行,那么第一个测试就会失败并出现NullPointerException as shown here。
package com.Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginPage {
WebDriver driver;
@FindBy(how = How.XPATH, using= (".//*[@id='fk-mainbody-id']/div/div/div[1]/div/div[4]/div[2]/input"))
WebElement email;
@FindBy(how = How.XPATH, using= (".//*[@id='fk-mainbody-id']/div/div/div[1]/div/div[4]/div[4]/input"))
WebElement pwd;
@FindBy(how = How.XPATH, using= (".//*[@id='fk-mainbody-id']/div/div/div[1]/div/div[4]/div[7]/input"))
WebElement loginbtn;
@FindBy(how = How.LINK_TEXT, using= ("My Account"))
WebElement myAccount;
public LoginPage (WebDriver driver) {
this.driver = driver;
}
public void enteremail (String Userid) {
email.sendKeys(Userid);
}
public void enterpwd(String passwd) {
pwd.sendKeys(passwd);
}
public void clickLoginButton() {
loginbtn.click();
}
public boolean checkMyAccount() {
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOf(myAccount));
return myAccount.isDisplayed();
}
}
package com.TestCases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
public class TestBase {
WebDriver driver;
@BeforeTest
public void invokeBrowser() {
driver = new FirefoxDriver();
driver.get("https://www.flipkart.com");
driver.manage().window().maximize();
}
@AfterTest
public void teardown() {
driver.quit();
}
}
package com.TestCases;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.Pages.HomePage;
public class VerifyHomePage extends TestBase
{
@Test
public void VerifyHomePageTitle() throws InterruptedException {
HomePage hpage = PageFactory.initElements(driver,HomePage.class);
System.out.println("Test Pass");
Assert.assertEquals(driver.getTitle(), "Online Shopping India | Buy Mobiles, Electronics, Appliances, Clothing and More Online at Flipkart.com");
hpage.clicklogin();
}
}
package com.TestCases;
import org.testng.annotations.Test;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import com.Pages.HomePage;
import com.Pages.LoginPage;
public class VerifyLogin extends TestBase {
@Test
public void checkLoginfunction() throws InterruptedException {
HomePage hpage = PageFactory.initElements(driver,HomePage.class);
hpage.clicklogin();
LoginPage loginp = PageFactory.initElements(driver,LoginPage.class);
loginp.enteremail("shreshtha.thakre@gmail.com");
loginp.enterpwd("Shreshtha!1");
loginp.clickLoginButton();
loginp.checkMyAccount();
Assert.assertTrue(loginp.checkMyAccount());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite">
<test name="Test">
<classes>
<class name="com.TestCases.VerifyHomePage"/>
<class name="com.TestCases.VerifyLogin"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
function square (x) {return x * x;}
function add3 (x) {return x + 3;}
function chainer(a) {
return function (b) {
for(var i = 0; i < a.length; i++) {
return a[i](b)
}
}
}
console.log(chainer([square, add3])(4));