Writing Selenium WebDriver Test Cases Part 3
Writing Selenium WebDriver Test Cases using User defined Methods/Reusable components.
I) Create User defined Methods
public degree Methods {
public static WebDriver driver;
//Launch Browser
public void launchBrowser(){
driver = novel FirefoxDriver();
}
//Admin Login without Parameters
public void adminLogin(){
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
}
//Admin Login With Parameters
public void adminLogin(String username, String password){
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("tdb1")).click();
}
//Close Browser
public void closeBrowser(){
if (! driver.toString().contains("null")){
driver.close();
}
}
public static void main(String[] args) {
Methods obj = novel Methods();
obj.launchBrowser();
obj.adminLogin();
obj.closeBrowser();
obj.launchBrowser();
obj.adminLogin("admin", "admin@123");
obj.closeBrowser();
}
}
--------------------------------------------------
II) Creating Test Cases using User defined Methods.
Test Case 1: Redirect to user Interface from Admin Interface
//Create Object/Instance
TestCase1 object = novel TestCase1();
object.launchBrowser();
object.adminLogin("admin", "admin@123");
driver.findElement(By.linkText("Online Catalog")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/")){
System.out.println("Redirected to User Interface -Passed");
}
else {
System.out.println("Redirected to User Interface -Failed");
}
object.closeBrowser();
--------------------------------------------------
Test Case 2: Admin Login Functionality alongside valid inputs(Positive Testing)
Test Case 2: Admin Login Functionality alongside valid inputs(Positive Test Case)
//Create Object/Instance
TestCase2 obj2 = novel TestCase2();
obj2.launchBrowser();
obj2.adminLogin();
String url = driver.getCurrentUrl();
if (url.contains("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Admin Login Successful - Passed");
}
else {
System.out.println("Admin Login Unsuccessful - Failed");
}
obj2.closeBrowser();
---------------------------------------------------
Test Case 3: Admin Login Functionality alongside invalid inputs (Negative Testing)
Test Case 3: Admin Login Functionality alongside invalid inputs(Negative Test Case)
//Create Object/Instance
TestCase3 obj3 = novel TestCase3();
obj3.launchBrowser();
obj3.adminLogin("admina", "admin@123a");
String ErrorMessage = driver.findElement(By.className("messageStackError")).getText();
if (ErrorMessage.contains("Error: Invalid administrator login attempt.")){
System.out.println("Handling Invalid Inputs - Passed");
}
else {
System.out.println("Not Handling Invalid Inputs - Failed");
}
obj3.closeBrowser();
-------------------------------------------------------
Write multiple Test Cases inwards a Program/Class
public degree TestCases extends Methods{
public static void main(String[] args) {
//Create Object/Instance
TestCases obj4 = novel TestCases();
//Test Case 1: Redirect to user Interface from Admin Interface
obj4.launchBrowser();
obj4.adminLogin("admin", "admin@123");
driver.findElement(By.linkText("Online Catalog")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/")){
System.out.println("Test Case 1: -Redirected to User Interface -Passed");
}
else {
System.out.println("Test Case 1: Redirected to User Interface -Failed");
}
obj4.closeBrowser();
//---------------------------------------------
//Test Case 2: Admin Login Functionality alongside valid inputs(Positive Test Case)
obj4.launchBrowser();
obj4.adminLogin();
String url2 = driver.getCurrentUrl();
if (url2.contains("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Test Case 2: Admin Login Successful - Passed");
}
else {
System.out.println("Test Case 2: Admin Login Unsuccessful - Failed");
}
obj4.closeBrowser();
//------------------------------------------
//Test Case 3: Admin Login Functionality alongside invalid inputs(Negative Test Case)
obj4.launchBrowser();
obj4.adminLogin("admina", "admin@123a");
String ErrorMessage = driver.findElement(By.className("messageStackError")).getText();
if (ErrorMessage.contains("Error: Invalid administrator login attempt.")){
System.out.println("Test Case 3: -Handling Invalid Inputs - Passed");
}
else {
System.out.println("Test Case 3: -Not Handling Invalid Inputs - Failed");
}
obj4.closeBrowser();
}
}
--------------------------------------------------------------
Synchronization
1) What is Synchronization?
General:
Process of coordinating or matching 2 or to a greater extent than activities /devices/processes inwards time.
Test Automation:
Process of matching the speeds of AUT (application Under Test) in addition to Test Tool inwards lodge to larn proper execution.
2) Why Synchronization is required?
During Test execution Test tool gives instructions i past times i alongside same speed, only AUT takes less fourth dimension for about steps execution in addition to to a greater extent than fourth dimension for about steps execution, inwards lodge to maintain them inwards Sync thus Synchronization is required.
3) Types of Synchronization
a) Unconditional Synchronization
In this nosotros specify timeout value, nosotros volition brand the tool to await for sure sum of fourth dimension in addition to thus proceed.
Syntax:
Thread.sleep(time inwards mille seconds);
Example:
Thread.sleep(9000);
b) Conditional Synchronization
i) It volition non run for all commands/statements inwards the application
ii) It industrial plant exclusively for findElement in addition to findElements statements
Syntax:
driver.manage().timeouts().implicitlyWait(Time inwards Seconds, TimeUnit.SECONDS);
4) Examples
//Unconditional Synchronization
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
Thread.sleep(10000);
driver.findElement(By.linkText("Gmail")).click();
//Conditional Synchronization
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.linkText("Gmail")).click();
------------------------------------------------------------------
Sumber http://www.gcreddy.com/
Writing Selenium WebDriver Test Cases using User defined Methods/Reusable components.
I) Create User defined Methods
public degree Methods {
public static WebDriver driver;
//Launch Browser
public void launchBrowser(){
driver = novel FirefoxDriver();
}
//Admin Login without Parameters
public void adminLogin(){
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
}
//Admin Login With Parameters
public void adminLogin(String username, String password){
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.id("tdb1")).click();
}
//Close Browser
public void closeBrowser(){
if (! driver.toString().contains("null")){
driver.close();
}
}
public static void main(String[] args) {
Methods obj = novel Methods();
obj.launchBrowser();
obj.adminLogin();
obj.closeBrowser();
obj.launchBrowser();
obj.adminLogin("admin", "admin@123");
obj.closeBrowser();
}
}
--------------------------------------------------
II) Creating Test Cases using User defined Methods.
Test Case 1: Redirect to user Interface from Admin Interface
//Create Object/Instance
TestCase1 object = novel TestCase1();
object.launchBrowser();
object.adminLogin("admin", "admin@123");
driver.findElement(By.linkText("Online Catalog")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/")){
System.out.println("Redirected to User Interface -Passed");
}
else {
System.out.println("Redirected to User Interface -Failed");
}
object.closeBrowser();
--------------------------------------------------
Test Case 2: Admin Login Functionality alongside valid inputs(Positive Testing)
Test Case 2: Admin Login Functionality alongside valid inputs(Positive Test Case)
//Create Object/Instance
TestCase2 obj2 = novel TestCase2();
obj2.launchBrowser();
obj2.adminLogin();
String url = driver.getCurrentUrl();
if (url.contains("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Admin Login Successful - Passed");
}
else {
System.out.println("Admin Login Unsuccessful - Failed");
}
obj2.closeBrowser();
---------------------------------------------------
Test Case 3: Admin Login Functionality alongside invalid inputs (Negative Testing)
Test Case 3: Admin Login Functionality alongside invalid inputs(Negative Test Case)
//Create Object/Instance
TestCase3 obj3 = novel TestCase3();
obj3.launchBrowser();
obj3.adminLogin("admina", "admin@123a");
String ErrorMessage = driver.findElement(By.className("messageStackError")).getText();
if (ErrorMessage.contains("Error: Invalid administrator login attempt.")){
System.out.println("Handling Invalid Inputs - Passed");
}
else {
System.out.println("Not Handling Invalid Inputs - Failed");
}
obj3.closeBrowser();
-------------------------------------------------------
Write multiple Test Cases inwards a Program/Class
public degree TestCases extends Methods{
public static void main(String[] args) {
//Create Object/Instance
TestCases obj4 = novel TestCases();
//Test Case 1: Redirect to user Interface from Admin Interface
obj4.launchBrowser();
obj4.adminLogin("admin", "admin@123");
driver.findElement(By.linkText("Online Catalog")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/")){
System.out.println("Test Case 1: -Redirected to User Interface -Passed");
}
else {
System.out.println("Test Case 1: Redirected to User Interface -Failed");
}
obj4.closeBrowser();
//---------------------------------------------
//Test Case 2: Admin Login Functionality alongside valid inputs(Positive Test Case)
obj4.launchBrowser();
obj4.adminLogin();
String url2 = driver.getCurrentUrl();
if (url2.contains("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Test Case 2: Admin Login Successful - Passed");
}
else {
System.out.println("Test Case 2: Admin Login Unsuccessful - Failed");
}
obj4.closeBrowser();
//------------------------------------------
//Test Case 3: Admin Login Functionality alongside invalid inputs(Negative Test Case)
obj4.launchBrowser();
obj4.adminLogin("admina", "admin@123a");
String ErrorMessage = driver.findElement(By.className("messageStackError")).getText();
if (ErrorMessage.contains("Error: Invalid administrator login attempt.")){
System.out.println("Test Case 3: -Handling Invalid Inputs - Passed");
}
else {
System.out.println("Test Case 3: -Not Handling Invalid Inputs - Failed");
}
obj4.closeBrowser();
}
}
--------------------------------------------------------------
Synchronization
1) What is Synchronization?
General:
Process of coordinating or matching 2 or to a greater extent than activities /devices/processes inwards time.
Test Automation:
Process of matching the speeds of AUT (application Under Test) in addition to Test Tool inwards lodge to larn proper execution.
2) Why Synchronization is required?
During Test execution Test tool gives instructions i past times i alongside same speed, only AUT takes less fourth dimension for about steps execution in addition to to a greater extent than fourth dimension for about steps execution, inwards lodge to maintain them inwards Sync thus Synchronization is required.
3) Types of Synchronization
a) Unconditional Synchronization
In this nosotros specify timeout value, nosotros volition brand the tool to await for sure sum of fourth dimension in addition to thus proceed.
Syntax:
Thread.sleep(time inwards mille seconds);
Example:
Thread.sleep(9000);
b) Conditional Synchronization
i) It volition non run for all commands/statements inwards the application
ii) It industrial plant exclusively for findElement in addition to findElements statements
Syntax:
driver.manage().timeouts().implicitlyWait(Time inwards Seconds, TimeUnit.SECONDS);
4) Examples
//Unconditional Synchronization
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
Thread.sleep(10000);
driver.findElement(By.linkText("Gmail")).click();
//Conditional Synchronization
WebDriver driver = novel FirefoxDriver();
driver.get("https://www.google.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.linkText("Gmail")).click();
------------------------------------------------------------------