Testing express

Testing express

Friday, May 22, 2015

Selenium tips: Locate elements that have same attributes for all elements


Some times a group of elements have same attributes for all elements, in that case if I want to click or select any specific element from that group, it will be hard using any location strategies. Here is one solution that I came up with

WebDriver driver = new FirefoxDriver();
driver.get("file:///D:/SameName.html");
driver.manage().window().maximize();
List<WebElement> cbox = driver.findElements(By.name("chk"));
cbox.get(cbox.size()-1).click();

In the above code we are getting all elements using locator name and value "chk" into a List
Then we are calling the get method on the list object by passing the index value and the performing the action.


Tuesday, May 19, 2015

Selenium : Mistakes and Learnings

I had a long pending issue in a selenium script. Problem was, after succesfully completing a flight booking transaction, I wanted to verify the confirmation message and get the pass result.This is the way I had implemented

public void isFlightConfirmationSuccessful(){
System.out.println(flightConfirmation.getText());
if(flightConfirmation.getText()==("Your itinerary has been booked!")){
System.out.println("Flight Booked Successfully");
if("more data" != null){
backToFlights.click();
}else
{
logOut.click();
}
}else{
System.out.println("Flight Not Booked");
}

}

Even though this method flightConfirmation.getText() would return the same string as expected "Your itinerary has been booked!" ,

still I would get the result false.

I was missing a java concept here "==" is not same as ".equals"

".equals" checks the equality of content
"==" checks the equality of references

This is the code that passes the test

public void isFlightConfirmationSuccessful(){
System.out.println(flightConfirmation.getText());
if(flightConfirmation.getText().equals("Your itinerary has been booked!")){
System.out.println("Flight Booked Successfully");
if("more data" != null){
backToFlights.click();
}else
{
logOut.click();
}
}else{
System.out.println("Flight Not Booked");
}
}


Sunday, May 17, 2015

Java Concepts - Strings literals

what is string literal?
it is a sequence of characters between quotations
Eg; "apple"

what is string literal/constant pool?
Its a collection of references to string objects
String objects that have references from string constant pool live on heap memory
String s = "apple";
s is a variable in string constant pool referencing to string object "apple" that lives on heap

if I write
String s1 = "apple";
s1 is another variable in string constant pool that is referencing to same object "apple"

s.equals(s1) // will return true because equals checks contents of objects
s==s1//will also return true because equality operator checks for referential equality

String s = "mango";
Here, reference of variable s is changing from "apple" to "mango", but "apple" object is still present in heap memory. Hence, strings objects are Immutable(cannot be changed)

s.equals(s1);//will return false because now s is referencing to object "mango" and s1 is referencing to object "apple", which are 2 different objects

String object referenced from the string literal pool is created when the class is loaded

String literals are not eligible for garbage collection, the object is always reachable through the intern() method.

source: http://www.javaranch.com/journal/200409/Journal200409.jsp#a1

Monday, May 4, 2015

Assertions in TestNG - HardAssert & SoftAssert

TestNG is a unit testing framework used by developers, but it has also gained popularity among testers.

It makes life easy of a tester while writing tests.
This post will be focused on one of the important features of TestNG called Assertions.

what is assertion?
A statement that asserts that a certain premise is true

In Testers language, Assertion is a check/validation to compare the expected and actual outcome.

All the assertions are part of the Assert class

Example of a assertion

Assert.assertEquals(boolean actual, boolean expected) 
Asserts that two booleans are equal.

More assertions can be found here : http://testng.org/javadoc/org/testng/Assert.html

There are 2 types of assertions

Hard assertions & Soft assertions

Hard Assertion : Throws an AssertException once Assert fails and continues with execution of next test in the suite. For example if you have a bunch of assertions in a @Test, If one assertion fails, the whole test fails.All assertions after the failed assertion are skipped once a assertion fails.

Soft Assertion;  soft assert allows to continue with next @Test without throwing any exception

Hard assert:
  @Test(priority = 2)
  public void ffPageElementsTestHardAssert() {
     Assert.assertTrue(ffPage.ff_Type_oneway.isDisplayed());
     Assert.assertTrue(ffPage.ff_Type_roundtrip.isDisplayed());
     Assert.assertTrue(ffPage.ff_PassengerCount.isDisplayed());
     Assert.assertTrue(ffPage.ff_DepartingFrom.isDisplayed());
     Assert.assertTrue(ffPage.ff_DepartingMonth.isDisplayed());
     Assert.assertTrue(ffPage.ff_DepartingDay.isDisplayed());
     Assert.assertTrue(ffPage.ff_ArrivingIn.isDisplayed());
     Assert.assertTrue(ffPage.ff_ReturningMonth.isDisplayed());
     Assert.assertTrue(ffPage.ff_ReturningDay.isDisplayed());
     Assert.assertTrue(ffPage.ff_ServiceClass_EC.isDisplayed());
     Assert.assertTrue(ffPage.ff_ServiceClass_BC.isDisplayed());
     Assert.assertTrue(ffPage.ff_ServiceClass_FC.isDisplayed());
     Assert.assertTrue(ffPage.ff_Airline.isDisplayed());
     Assert.assertTrue(ffPage.ff_Continue.isDisplayed());   
  }
Soft assert:
  SoftAssert sa = new SoftAssert();
@Test(priority = 2)
  public void ffPageElementsTestHardAssert() {
     sa.assertTrue(ffPage.ff_Type_oneway.isDisplayed());
     sa.assertTrue(ffPage.ff_Type_roundtrip.isDisplayed());
     sa.assertTrue(ffPage.ff_PassengerCount.isDisplayed());
     sa.assertTrue(ffPage.ff_DepartingFrom.isDisplayed());
     sa.assertTrue(ffPage.ff_DepartingMonth.isDisplayed());
     sa.assertTrue(ffPage.ff_DepartingDay.isDisplayed());   
  }
Soft assertions can be helpful when we are validation forms.