Simply Selenium!!

Day 6:Flexible Tests- Positive and Negative Tests

I have chosen Facebook website for checking two scenarios:

  1. Successful login(Positive)
  2. Incorrect email login(Negative)

Step 1:Create new classes as below:

Step 2:FacebookMainPage.java: Has URL, Title and sendkeys to input email id and password:

package com.data;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.testng.Assert;

public class FacebookMainPage {

public static final String PAGE_URL=”https://www.facebook.com";
 public static final String PAGE_TITLE=”Facebook — Log In or Sign Up”;

@FindBy(id=”email”) WebElement field_EmailLogin;
 @FindBy(id=”pass”) WebElement field_passwordLogin;
 @FindBy(id=”u_0_m”) WebElement button_click;
 WebDriver driver;

//Constructor 
 public FacebookMainPage(WebDriver driver){
 this.driver=driver;
 }

public void setText_EmailLogin(String text){
 field_EmailLogin.sendKeys(text);
 Assert.assertEquals(field_EmailLogin.getAttribute(“value”),text);
 }

public void setText_passwordLogin(String text){
 field_passwordLogin.sendKeys(text);
 Assert.assertEquals(field_passwordLogin.getAttribute(“value”),text);
 }
 
 public void clickLoginMain(){
 button_click.click();
 }
}

Obtaining value for :

@FindBy(id=”email”) WebElement field_EmailLogin;

@FindBy(id=”pass”) WebElement field_passwordLogin;

@FindBy(id=”u_0_m”) WebElement button_click;

Step 3: Facebookdata.java : Has multiple data set.

package com.data;

import org.testng.annotations.DataProvider;

public class Facebookdata {

@DataProvider(name=”login”)
 public static Object [][] login(){
 return new Object[][]{

//For Positive Test
 {“Your Email ID”,”Your Password”,null},

//For Negative Test
 {“Your incorrect Email ID”,”Your Password”,”The email address that you’ve entered doesn’t match any account. Sign up for an account.”},
 };

}
}

Step 4: FacebookMainFeed.java : Validations for Successful login to Facebook main feeds page

package com.data;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class FacebookMainFeed {
 @FindBy(css =”.innerWrap”)WebElement success_searchFacebook;
 public String getUsernameText(){
 System.out.println(“Success:”+success_searchFacebook.getText());
 return success_searchFacebook.getText();
 }
}

Validate any web element on FB feeds page as below:

@FindBy(css =”.innerWrap”)

  1. Highlight ‘Search Facebook’ bar using firebug and obtain the class name for it.

Step 5:FacebookLoginPage.java: To check if email id and password are entered correctly in login page.

package com.data;

import java.util.NoSuchElementException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.testng.Assert;

public class FacebookLoginPage {

public static final String PAGE_URL=”https://www.facebook.com/login.php";
 public static final String PAGE_TITLE=”Facebook”;
 @FindBy(css=”._1tp8") WebElement errorHeader;
 WebDriver driver;

//Constructor 
 public FacebookLoginPage(WebDriver driver){
 this.driver=driver;
 }

public boolean checkErrorHeader(String text){
 try
 {
 return errorHeader.getText().equals(text); 
 }
 catch(NoSuchElementException e)
 {
 return false;
 }

}
}

loginPage: When incorrect email ID and password is entered below error message will be populated. For negative scenario we will validate the error message.

@FindBy(css=”._1tp8") WebElement errorHeader; — Can be obtained as below:

Step 6:Login_test.java- Tests logic is coded here

package com.facebook.tests;
import static org.testng.AssertJUnit.assertTrue;
import static org.testng.AssertJUnit.assertTrue;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.*;

import com.data.FacebookLoginPage;
import com.data.FacebookMainFeed;
import com.data.FacebookMainPage;
import com.data.Facebookdata;

public class Login_test{

public WebDriver driver;
 FacebookMainPage fbMainPage;
 FacebookMainFeed fbMainFeed;
 FacebookLoginPage fbLoginPage;

@BeforeClass(alwaysRun=true)
 public void setUp()
 {
 this.driver=new FirefoxDriver();
 fbMainPage=PageFactory.initElements(driver, FacebookMainPage.class);
 fbLoginPage=PageFactory.initElements(driver, FacebookLoginPage.class);
 fbMainFeed=PageFactory.initElements(driver, FacebookMainFeed.class);
 }
 @AfterClass(alwaysRun=true)
 public void tearDown()
 {//Close Driver
 this.driver.quit();
 }

@Test(groups={“p1”},dataProviderClass=Facebookdata.class,dataProvider=”login”)
 public void testLoginPage(String email, String password, String errorType){
 driver.manage().deleteAllCookies();
 driver.get(fbMainPage.PAGE_URL);
 Assert.assertEquals(driver.getTitle(),fbMainPage.PAGE_TITLE);
 fbMainPage.setText_EmailLogin(email);
 fbMainPage.setText_passwordLogin(password);
 fbMainPage.clickLoginMain();

//Negative Test

if(!StringUtils.isBlank(errorType))
 {
 boolean result = fbLoginPage.checkErrorHeader(errorType);
 Assert.assertTrue(result,”Expected result:-”+errorType);
 }
 //Positive test

else
 {
 assertTrue(!fbMainFeed.getUsernameText().isEmpty());
 }
 }
}

Result:

Success:Search Facebook
PASSED: testLoginPage(“youremail@gmail.com”, “pass@123”, null)
PASSED: testLoginPage(“incorrectyouremail@gmail.com”, “pass@123”, “The email address that you’ve entered doesn’t match any account. Sign up for an account.”)

===============================================
 Default test
 Tests run: 2, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@340f438e: 117 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6442b0a6: 15 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@57829d67: 122 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 12 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@7f690630: 10 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@2acf57e3: 11 ms