Monday 29 October 2018

Page Factory

What is Page Factory?

Page Factory is an inbuilt Page Object Model concept for Selenium WebDriver but it is very optimized.


Here as well, we follow the concept of separation of Page Object Repository and Test Methods. Additionally, with the help of PageFactory class, we use annotations @FindBy to find WebElement. We use initElements method to initialize web elements

EXAMPLE :

step:1 Create two new packages and give name pages and tests
           right click on src->new->packages

step:2 Crate pom of pages
           right click on pages->new-.class
            here you give class name "Loginpage"

Login page using page factory

--------------------------------------------------------------------------------------------------------------------
public class Loginpage {

 WebDriver driver;

@FindBy(xpath="html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[1]/in  put"]")
WebElement mail;

 @FindBy(xpath="html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[2]/input"]");
WebElement pass;

@FindBy(xpath="html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[3]/label/input");
WebElement login;

public Homepage(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(driver, this);
                

}
//create constructor of class with pagefactory

public void getmail(String mailid)
{
mail.sendKeys(mailid);

}
//make method of get value of emaild id

public void getpass(String password)
{
mail.sendKeys(password);

}


public void log()
{
               login.click();
}
//make method for click on loging button

//now we make one common method who provide all in one

public void logintofb(String mailid, String password)
{
this.getmail(mailid);
                this.getpass(password);
this.log();
}


--------------------------------------------------------------------------------------------------------------------
now we create all pages which need in testing

Step:4 make main java class in tests package



package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import pages.Loginpage;
import pages.fbhomepage;

public class Setup {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\vishal\\selenium\\chromedriver_win32\\dasdf\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.facebook.com");
Loginpage objlog;
                 //create object of page
objlog = new Loginpage(driver);
objlog.logintomail("vishallaniya1997", "vlaniya@123");
Thread.sleep(3000);
driver.close();

}

}


POM(page objective modeling)

POM (Page Objective Modeling)
  • Page Object Model is a design pattern to create Object Repository for web UI elements.
  • Under this model, for each web page in the application, there should be corresponding page class.
  • This Page class will find the Web Elements of that web page and also contains Page methods which perform operations on those Web Elements.
  • Name of these methods should be given as per the task they are performing, i.e., if a loader is waiting for the payment gateway to appear, POM method name can be waitForPaymentScreenDisplay().

Advantages of POM

  1. Page Object Patten says operations and flows in the UI should be separated from verification. This concept makes our code cleaner and easy to understand.
  2. The Second benefit is the object repository is independent of test cases, so we can use the same object repository for a different purpose with different tools. For example, we can integrate POM with TestNG/JUnit for functional Testing and at the same time with JBehave/Cucumber for acceptance testing.
  3. Code becomes less and optimized because of the reusable page methods in the POM classes.
  4. Methods get more realistic names which can be easily mapped with the operation happening in UI. i.e. if after clicking on the button we land on the home page, the method name will be like 'gotoHomePage()'.    

EXAMPLE :

step:1 Create two new packages and give name pages and tests
           right click on src->new->packages

step:2 Crate pom of pages
           right click on pages->new-.class
            here you give class name "Loginpage"

Login page pom file
--------------------------------------------------------------------------------------------------------------------
public class Loginpage {
WebDriver driver;
By mail = By.xpath("html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[1]/input");
By pass= By.xpath("html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[2]/input");
By login =By.xpath("html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[3]/label/input");
//you also use name , id ,css and other behalf of xpath

public Homepage(WebDriver driver)
{
this.driver = driver;
}
//here we create constructor of class

public void getmail(String mailid)
{
driver.findElement(mail).sendKeys(mailid);
//make method of get value of emaild id

public void getpass(String password)
{
driver.findElement(pass).sendKeys(password);
//make method of get value of password

public void log()
{
                driver.findElement(login).click();
}
//make method for click on loging button

//now we make one common method who provide all in one

public void logintofb(String mailid, String password)
{
this.getmail(mailid);
                this.getpass(password);
this.log();
}


--------------------------------------------------------------------------------------------------------------------
now we create all pages which need in testing

Step:4 make main java class in tests package

package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import pages.Loginpage;
import pages.fbhomepage;

public class Setup {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\vishal\\selenium\\chromedriver_win32\\dasdf\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.facebook.com");
Loginpage objlog;
                 //create object of page
objlog = new Loginpage(driver);
objlog.logintomail("vishallaniya1997", "vlaniya@123");
Thread.sleep(3000);
driver.close();

}

}

Friday 26 October 2018

Test Scenario vs Test Condition

What is Test Scenario?

A Test Scenario is a probable way or method to test an Application.

What is a Test Condition?

Test Condition is the specification that a tester must follow for testing an Application. There can be multiple Test Conditions in a Test Scenario.
Difference between Test Scenario and Test Condition is a very common FAQ amongst QA beginners.
Below is a detailed comparison
Test Scenario
Test Condition
  • Test scenario is a possible ways to test an application.
  • Test condition is the constraint that you should follow to test an application.
  • Test scenario can be a single or a group of test cases
  • Test condition can be a piece of functionality or anything you want to verify. In simple terms the goal of a test cases
  • It is important when time is less and most team members understand the details from one line scenario
  • It is an item or event of a system that could be verified by one or more test cases. Eg; transaction, function, structural element etc.
  • Good Test coverage can be achieved by dividing application in test scenarios which reduces the complexity
  • Good Test Condition ensure system is bug free
  • Test scenario are rather vague and covers wide range of possibilities
  • Test condition are very specific
Example: For testing you have so many ways like positive testing, negative testing, BVA etc.Example: When User Name and Password are valid then application will move forward

Thursday 25 October 2018

Alert handling in selenium webdriver

Simple alert

Simple alerts just have a OK button on them. They are mainly used to display some information to the user. The first alert on our test page is a simple alert. Following code will read the text from the Alert and then accept the alert.

Code:
package alert;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SimpleAlert {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\vishal\\selenium\\chromedriver_win32\\dasdf\\chromedriver.exe");     
WebDriver driver= new ChromeDriver();
    driver.get("http://toolsqa.com/handling-alerts-using-selenium-webdriver/");
    
    WebElement simplealert=driver.findElement(By.xpath("html/body/div[1]/div[5]/div[2]/div/div/p[4]/button"));
    simplealert.click();
    //click on the simple alert button
    Alert alert=driver.switchTo().alert();
    String message=alert.getText();
    System.out.println("Alert="+message);
    alert.accept();
    
}

}

--------------------------------------------------------------------------------------------------------------------------

Confirmation Alert

This alert comes with an option to accept or dismiss the alert. To accept the alert you can use Alert.accept() and to dismiss you can use the  Alert.dismiss()

Code:
package alert;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ConfirmationAlert {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\vishal\\selenium\\chromedriver_win32\\dasdf\\chromedriver.exe");     
WebDriver driver= new ChromeDriver();
    driver.get("http://toolsqa.com/handling-alerts-using-selenium-webdriver/");
    
    WebElement confirmationalert=driver.findElement(By.xpath("html/body/div[1]/div[5]/div[2]/div/div/p[8]/button"));
    confirmationalert.click();
    Alert alert=driver.switchTo().alert();
    String message=alert.getText();
    System.out.println("Alert="+message);
    alert.accept();
    //you also use alert.dismiss() comand for alert
    driver.close();
}

}

------------------------------------------------------------------------------------------------------

Prompt Alerts

In prompt alerts you get an option to add text to the alert box. This is specifically used when some input is required from the user. We will use the sendKeys() method to type something in the Prompt alert box. Here is the code

Code:

package alert;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class PromptAlert {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\vishal\\selenium\\chromedriver_win32\\dasdf\\chromedriver.exe");     
WebDriver driver= new ChromeDriver();
    driver.get("http://toolsqa.com/handling-alerts-using-selenium-webdriver/");
    
    WebElement promptalert=driver.findElement(By.xpath("html/body/div[1]/div[5]/div[2]/div/div/p[11]/button"));
    promptalert.click();
    
    
    Alert alert=driver.switchTo().alert();
    //for entering message
    driver.switchTo().alert().sendKeys("say hello");
    
    String message=alert.getText();
    System.out.println("Alert="+message);
    alert.accept();
    //you also use alert.dismiss() comand for alert
    driver.close();

}

}


Tuesday 23 October 2018

Globalization Testing Vs Localization Testing

Globalization Testing

Globalization testing is to ensure that application can function in any culture or locale (language, territory and code page) It is also called as Internationalization Testing.

 Localization Testing
Localization testing is the software testing process for checking the localized version of a product for that particular culture or locale settings.  The areas affected by localization testing are UI and content.

Globalization Testing
Localization Testing
  • Globalization testing checks proper functionality of the product, using every type of international input possible. It ensures that without breaking functionality the code can handle all international support .For example I18N, is the process of planning and implementing products and services so that they can easily be adapted to specific languages and culture.
  • Localizing testing is done to ensure the quality of a product for a particular target or locale. For example, for French users the testing product is denoted as L10N.
  • In a globalized product, code is separated from the messages or information. With the help of globalization, it enables software to be used with different languages without having to redesign the complete software.
  • This is not necessary in a Localized product
  • Globalization focuses your applications capabilities on users as generic user base.
  • Localization focuses onsubset of users in a given culture or locale.
  • Separation of testers from translators and engineers, ensuring a thorough and impartial approach.
  • It helps reduce time for testing since its done for just on locale
  • Formalized bug reporting
  • It reduces overall testing and support costs
  • Detect potential problems in application design that could inhibit globalization
  • Validation of all application resources
  • It ensures that without breaking functionality code can handle all international support
  • Verification of linguistic accuracy and resource attributes. Check Typographical errors
  • Verification of linguistic accuracy and resource attributes Compatibility tests of
  • Consistency checking of printed documentation, messages, command key sequence etc.
  • Compatibility tests of hardware and application according to the product’s target region
  • Confirmation of input and display environment standards , adherence to system. Usability of User Interface

Positive Vs Negative testing

Positive testing


Positive testing is the type of testing that can be performed on the system by providing the valid data as input. It checks whether an application behaves as expected with positive inputs.

For example -
There is a text box in an application which can accept only numbers. Entering values up to 99999 will be acceptable by the system and any other values apart from this should not be acceptable. To do positive testing, set the valid input values from 0 to 99999 and check whether the system is accepting the values.

Negative testing

Negative Testing is a variant of testing that can be performed on the system by providing invalid data as input. It checks whether an application behaves as expected with the negative inputs. This is to test the application does not do anything that it is not supposed to do so.
For example -
Negative testing can be performed by entering characters A to Z or from a to z. Either software system should not accept the values or else it should throw an error message for these invalid data inputs.

Verification v/s Validation in Software testing

Verification v/s Validation in Software testing
 VerificationValidation
  • Verifying process includes checking documents, design, code and program
  • It is a dynamic mechanism of testing and  validating the actual product
  • It does not involve executing the code
  • It always involves executing the code
  • Verification uses methods like reviews, walkthroughs, inspections and desk- checking etc.
  • It uses methods like Black Box Testing,White Box Testing and non-functional testing
  •  Whether the software conforms to specification is checked
  • It checks whether software meets the requirements and expectations of customer
  • It finds bugs early in the development cycle
  • It can find bugs that the verification process can not catch
  • Target is application and software architecture, specification, complete design, high level and data base design etc.
  • Target is actual product
  • QA team does verification and make sure that the software is as per the requirement in the SRS document.
  • With the involvement of testing team validation is executed on software code.
  • It comes before validation
  • It comes after verification

Monday 22 October 2018

Alert handling in selenium webdriver


import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class KeyandMouse {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\vishal\\selenium\\chromedriver_win32\\dasdf\\chromedriver.exe");
//path of exe file of chromedriver
WebDriver driver = new ChromeDriver();
//create object of webdriver as a driver name
String url="http://demo.guru99.com/test/delete_customer.php";
driver.get(url);
//you also write direct url in driver.get("https://www.amazon.in/");


driver.findElement(By.xpath("http://demo.guru99.com/test/delete_customer.php")).sendKeys("abcd");
//we find xpath of customer id and pass value
driver.findElement(By.xpath("html/body/form/table/tbody/tr[3]/td[2]/input[1]")).click();
//here we find xpath of submit button and perforn click event

//when you click that time you see alert box we directly not handle that alert box
Alert alert = driver.switchTo().alert();
//here we switch control to alert box

String alertmesage=driver.switchTo().alert().getText();
System.out.println(alertmesage);
//we take alert message and print
//it is not important to perfoem we need just want to see alert message that why we perforn

alert.accept();
//alert.dismiss(); if you want to dismiss the alert then we use this command




}

}

Keyboard and mouse event using selenium webdriver

Keyboard & Mouse Events

Handling special keyboard and mouse events are done using the Advanced User Interactions API. It contains the Actions and the Action classes that are needed when executing these events. The following are the most commonly used keyboard and mouse events provided by the Actions class.
MethodDescription
clickAndHold()Clicks (without releasing) at the current mouse location.
contextClick()Performs a context-click at the current mouse location.
doubleClick()Performs a double-click at the current mouse location.
dragAndDrop(source, target)Performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.

Parameters:

source- element to emulate button down at.

target- element to move to and release the mouse at.
dragAndDropBy(source, x-offset, y-offset)Performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.

Parameters:

source- element to emulate button down at.

xOffset- horizontal move offset.

yOffset- vertical move offset.
keyDown(modifier_key)Performs a modifier key press. Does not release the modifier key - subsequent interactions may assume it's kept pressed.

Parameters:

modifier_key - any of the modifier keys (Keys.ALT, Keys.SHIFT, or Keys.CONTROL)
keyUp(modifier _key)Performs a key release.

Parameters:

modifier_key - any of the modifier keys (Keys.ALT, Keys.SHIFT, or Keys.CONTROL)
moveByOffset(x-offset, y-offset)Moves the mouse from its current position (or 0,0) by the given offset.

Parameters:

x-offset- horizontal offset. A negative value means moving the mouse left.

y-offset- vertical offset. A negative value means moving the mouse down.
moveToElement(toElement)Moves the mouse to the middle of the element. 

Parameters:

toElement- element to move to.
release()Releases the depressed left mouse button at the current mouse location
sendKeys(onElement, charsequence)Sends a series of keystrokes onto the element. 

Parameters:

onElement - element that will receive the keystrokes, usually a text field

charsequence - any string value representing the sequence of keystrokes to be sent
--------------------------------------------------------------------------------------------------------------------------


//We are search a product on amazon using keyboard and mouse event
//we try to search "mobile phone " in amazon

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;

import org.openqa.selenium.interactions.Actions;
public class KeyandMouse {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\vishal\\selenium\\chromedriver_win32\\dasdf\\chromedriver.exe");
//path of exe file of chromedriver
WebDriver driver = new ChromeDriver();
//create object of webdriver as a driver name
String url="https://www.amazon.in/";
driver.get(url);
//you also write direct url in driver.get("https://www.amazon.in/");

//we declare a webelement of search box of amazon site

WebElement searchbox= driver.findElement(By.xpath(".//*[@id='twotabsearchtextbox']"));


//we want to use mouse and keyboard event so we need Actions library
Actions action=new Actions(driver);
//we create object of actions

Action search=action.moveToElement(searchbox)//couser is move to searchbox
.keyDown(searchbox,Keys.SHIFT)//after move is perform shift key press
.sendKeys("m") //m is write but shift key is press so m is written in capital word
.keyUp(searchbox, Keys.SHIFT) //shift key is release
.sendKeys("obile ")             //after that search box is take obile
.keyDown(searchbox,Keys.SHIFT)
.sendKeys("p")
.keyUp(searchbox, Keys.SHIFT)
.sendKeys("hone")
.build(); //build is use when we want to perform more then 2 actions

search.perform();//search action is perform
Thread.sleep(1000);
driver.close();
}

}

Jmeter

Database Testing: (1) Add mysql JDBC to Jmeter lib folder and restart Jmeter (2) Right click on test name -> add  Thread group (3)...