dimanche 31 juillet 2016

Tests are failing if run through TestNG xml, but works if run it independently

I have got two tests which i am attempting run through testNG xml. If i run those independently, it works. But 1 st test is failing if i run it as a suit with xml. I have 2 classes for 2 pages - HomePage & LoginPage which has web elements and its method 1 TestBase class which invokes the browser 2 tests - VerifyHomePage - in which i am validating the tile of the page VerifyLoginPage - Validating the login functionality.

HomePage Class -

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;
public class HomePage 
    {
    WebDriver driver;
     @FindBy(how = How.LINK_TEXT, using = ("Log In"))
     WebElement loginlink;
     public HomePage(WebDriver driver)
    {
        this.driver = driver;
    }
    public  void clicklogin()
    {
        loginlink.click();
    }
}


LoginPage Class -

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();
    }
}


TestBase Class -

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()
{
    //System.setProperty("webdriver.chrome.driver", "/Users/shreshthathakre/Documents/JAR Files/chromedriver");
    driver = new FirefoxDriver();
    //driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get("http://ift.tt/NDSSYa");
    driver.manage().window().maximize();    
}
@AfterTest
public  void teardown()
{
    driver.quit();
}
}


VerifyHomePage Test -

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();
    }
}


VerifyLogin Test -

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());
    }
}

Aucun commentaire:

Enregistrer un commentaire