Set up the first Web Driver project in Eclipse

Setting up the first Web Driver project in Eclipse is very easy.

Please follow the next checklist for it:


1. download the Web Driver client and server ZIP files  and extract them in a folder



2. download the Chrome Web Driver ZIP file

3. extract the EXE file from the archive and copy it in the Chrome browser home folder





4. create a project in Eclipse


5. add the server and client JAR files to the project in Eclipse



6. create a basic test case

import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestClass {

public static WebDriverWait wait;
public static WebDriver driver;

@BeforeClass
public static void setUp() 
{
System.setProperty("webdriver.chrome.driver",       "C:\\Users\\asiminiuc\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"); 

driver = new ChromeDriver();            
}

@AfterClass
public static void tearDown() 
{
driver.quit();
}

@Test
public void testMethod() throws InterruptedException
{

driver.get("http://www.google.ca");

WebElement element;

element = driver.findElement(By.xpath("//input[@id='gbqfq']"));
assertTrue(element.isDisplayed() == true);
}
}

7. run the project


Unit Testing for test automation

In order to create automated tests for an application, a tester should have decent knowledge of a few things:

- a programming language (Java, Ruby, C#)

- knowledge of the test automation API (let's say WebDriver)

- application/browser DOM 

- XPATH

- unit testing framework (NUNIT, JUNIT)


Some questions that can come up while creating the test automation scripts are related to unit testing:

- What should be tested?

- How should the testing be done?

- How many assertions should be included in a test script?

- How much test code should be written?

- When should the test code be executed?

- What are the characteristics of a good unit test?


For answers for these questions and much more, I recommend the following book:

Pragmatic Unit Testing in C# with NUNIT
by Andrew Hunt, David Thomas


It is a very good presentation of unit testing with NUNIT and C#.

All book's concepts apply as well to other unit testing frameworks and languages, like JUNIT and Java.

This is a checklist extracted from the book with unit testing practices:


General Principles

1. test anything that might break
2. test everything that does break
3. new code is guilty until proven innocent
4. write at least as much test code as production code
5. run local tests with each compile
6. run all tests before check-in to repository


What to test: use your RIGHT-BICEP

1. are the results RIGHT?
2. are all the Boundary conditions CORRECT?
3. can you check Inverse relationships?
4. can you Cross-Check results using other means?
5. can you force Error conditions to happen?
6. are Performance characteristics within bounds?


Questions To Ask:

1. if the code ran correctly, how would I know?
2. how am I going to test this?
3. what else can go wrong?
4. could this type of problem happen anywhere else?


Good tests are A TRIP

Automatic
Thorough
Repeatable
Independent
Professional


CORRECT Boundary Conditions

Conformance - does the value conform to an expected format?

Ordering - is the set of values ordered or unordered as appropriate?

Range - is the value within reasonable minimum and maximum values?

Reference - does the code reference anything external that isn't under direct control of the code itself?

Existence - does the value exist? (e.g., is non-null, non-zero, present in a set, etc)

Cardinality - are there exactly enough values?

Time (absolute and relative) - is everything happening in order? at the right time? in time?

Find Page Elements In Chrome With XPATH

Sometimes, the test automation of a site needs to be done in Chrome instead of Firefox.

This may be because the site works better (or only) in Chrome or because the site needs to be tested on multiple browsers.

How can site elements be found with XPATH in Chrome?

Firefox has the FireBug and FirePath plugins that help greatly with the XPATH expressions.

But there are no good equivalent browser extensions for Chrome (or I have not found them yet).

So what to do in Chrome?

The Chrome Developer Tools are the solution.

See below the steps to take:

1. click on the browser menu 
2. click Tools
3. click Developer Tools



4. click the CONSOLE tab



5. type the following in the Console tab:

$x("xpath expression")




Enjoy!



_____________________________________________________________________________


Do you want to learn more about test automation and Java?
I will start an SELENIUM online group training on October 15.
Please see the training details here.

How to handle windows site authentication in Web Driver?

I have started recently to work on test automation for a site that requires user authentication.

The authentication is not done through a login page but through integrated windows security.

This means that, before the site loads, a popup is displayed asking for the account's username and password.

The first idea that I had was to follow the standard way of working with popups in Selenium. 

You can see details on this link from StackOverFlow.com:

How to handle pop up in selenium webdriver using java


The code with popup handling did not work as the popup is not in the browser DOM when it is displayed.

The second idea was to populate the popup using another tool like AutoIt.

Not the best solution, though.

After more searching, I found something that works.

Instead of opening the page with this line

                  driver.get("http://www.samplesite.com/parameter");

use 

                  driver.get("http://username:password@www.samplesite.com/parameter");


When opening the site with the url updated with username and password, the authentication happens automatically and the site is loaded in the browser.





_____________________________________________________________________________


Do you want to learn more about test automation and Java?
I will start an SELENIUM online group training on October 15.
Please see the training details here.

Web Service testing with SOAP UI

Why learn web service testing?


A few reasons are that:

- most of the projects for integrating applications from different companies use web services for passing information from one side to the other

- if your company has a website for selling, lets say, furniture and the company has also mobile apps and mobile websites (iOS, Android, Windows), chances are high that web services are used for implementing functionality needed by all apps 

- if your company uses off-the-shelf products that implement business processes, chances are high that these products use web services


Now that you understand the significance of web service testing, let's look into how difficult it is to learn it.


Most of the tools for web service testing use well-known technologies like XML, XPATH and programming languages.

So if you have decent knowledge of XPATH and Java from other uTest university courses, you can apply them for web service testing.

Also, having some exposure to test automation and load testing is useful since there is web service test automation and web service load testing.



The tool that will be the focus of this course is SOAP UI from Smart Bear.

There is a free version that has 90% of functionalities and also a paid version.

The free version will be used in the course.



I wrote a short introduction for SOAP UI a few years ago.

You can find the links below:

http://university.utest.com/soap-ui-web-services-testing-the-basics/
http://university.utest.com/soap-ui-web-service-testing-a-practicum/



Lots of new things were added since then and we will explore them together in this course.


The course will try to be as hands on as possible and have videos included so that you can practice at home the new skills.


For any questions on the content of this course, please email me at alex@alexsiminiuc.com

Convert Selenium projects from RC (selenium 1.0) to Web Driver (selenium 2.0)

Why convert Selenium projects from RC to Web Driver?

A few reasons are that 

1. Web Driver has almost all development support now and RC is slowly being "retired"

2. Web Driver interacts natively with the browsers and not through a proxy like RC; 
             More details on how RC simulates the interaction with the browser

3. Web Driver allows writing object oriented code

4. No server needs to be started for Web Driver

5. The scripts performance is much better when using the html, in memory driver
           More details on the RC - Web Driver differences



The first thing to do when converting the RC project to Web Driver is code re-factoring, making the code as simple and as efficient as possible:

1. All code repetitions should be removed

2. Page object classes should be created

          More details on page object classes:

                       Selenium Basics - why is page object needed?

                       Selenium RC - code refactoring and page object model

3. All code that interacts with the page (through Selenium commands) should be removed from the test methods and placed in the page object classes



After refactoring the project, the number of lines that include Selenium commands will be reduced considerably. 

The remaining lines that include Selenium commands will need to be modified for the Web Driver API.

No changes should be needed for the test methods.





The Web Driver API is as clear as the RC API.

See below RC and Web Driver code that do the same thing:


Select a value in a list

RC
selenium.select(listPath, listItem);
assertTrue(selenium.getSelectedLabel(listPath).equalsIgnoreCase(listItem) == true);

Web Driver
Select ddown = new Select(driver.findElement(By.xpath(listPath)));
dd.selectByVisibleText(listItem);
assertTrue(dd.getFirstSelectedOption().getText().equalsIgnoreCase(listItem) == true);



Type a value in a textbox

RC
selenium.type(textboxPath, textboxValue);
assertTrue(selenium.getValue(textboxPath).equalsIgnoreCase(textboxValue) == true);

Web Driver
WebElement element = driver.findElement(By.xpath(textboxPath));
element.sendKeys(textboxValue);         
assertTrue(element.getAttribute("value").equalsIgnoreCase(textboxValue) == true);
             


Check a checkbox

RC
selenium.check(checkbox);
assertTrue(selenium.isChecked(checkbox) == true);

Web Driver
WebElement checkbox = driver.findElement(By.xpath(checkboxPath));
checkbox.click();
assertTrue(checkbox.isSelected() == true);
   


Click a link or a button

RC
selenium.click(includeAddSectionLinkXPATH);

Web Driver
driver.findElement(By.xpath(includeAddSectionLinkXPATH)).click();
               


Check if an element is visible

RC
return selenium.isVisible(elementPath);

Web Driver
assertTrue(driver.findElement(By.xpath(elementPath)).isDisplayed() == true);



Get the length of an HTML element value

RC
return (selenium.getText(elementPath).length() > 0);

Web Driver
Integer fieldLength = driver.findElement(By.xpath(elementPath)).getText().length();