Be lazy - automate your web routines with Selenium

Few years ago i work on a project that once in a few days (sometimes even few times a day) all developers stop their work and do a sanity test on the web-product. I suggest few times to automate the procedure with Selenium but my suggestion wasn't accepted.
I looked on the short term and didn't invest my own time to create automate sanity tests.

Few years latter i got a mission to automate a browser tasks. I learn Selenium in order to do this mission and I was so angry about myself that I didn't did it few years ago and save months of development time.

Since i automate web-flow on complex web pages (Ajax, Generated Id...) i wasted lot of hours to overcome on some selenium pitfalls, like frames, Ajax,read only attributes ... The good news is that few weeks ago a new and great selenium book was published - "Selenium Testing Tools Cookbook".

This is not a Selenium tutorial. You will find lots of them in the web. The target is a quick introduction to show you how simple it is.

There ate 2 main ways to run Selenium automation either by using the Selenium IDE or by using Selenium WebDriver.

Selenium IDE:
Selenium IDE is an integrated development environment for performing Selenium tests.It can be added as an addon to firefox. 
You can open Selenium IDE from Firefox click the record button and record the steps that you are executing in Firefox.
Each action that executed in the Firefox will be recorded as a step in the recorded test case. While recording and after recording you can go to the IDE and edit the steps by add steps manually or change the step action.

A classic Selenium sequence is locating a WebElement and than take an action on the WebElement.
Selenium IDE in record mode record how to locate the WebElement and the action that was taken on the WebElement. 
Lets see a simple flow:
1. open Google
2. type " hello selenium " in the search box
3. click on search

I will focus on step 2. In step 2 we need to locate the search box input text and do a type action on it.
This is how it look on the IDE:

You can see that the command action is "type" and the target (WebElement) is element with id "gbqfq".
The IDE need to record how to locate the target. There are few ways to locate element on a webpage.
You can locate it by Id, name,css,dom,xpath ... 
The IDE will chose one of the way above to record how to locate the element. The IDE try first to record id locator than name locator ... Id locator is a good choice for element locator because element has unique id, however id locator will not always work. In some cases the element doesn't has ID or the ID is auto generated , so next time we will run the test we will get an exception that WebElement wasn't found. The IDE cannot guess it, so in some cases we need to go to the step and chose another locator. In the picture above you can see that the target drop-down contain different locator for the input text element.

After we record the test we can run it from the IDE or we can export it to one of the available WebDriver API (Java,C#,Pyton...)
In the IDE we go to File->Export Test Case As and chose one of the API that supported there.

A python code that execute the following will look like this:

        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("gbqfq").clear()
        driver.find_element_by_id("gbqfq").send_keys("hello selenium")
        driver.find_element_by_id("gbqfba").click()


The disadvantage of the IDE is the missing supported in other browser than Firefox. Most of the popular Browser have Selenium Driver, so you can run your test on most of the browsers.
Selenium API is very simple , few hours will make you familiar with the API and you will see that its simpler and faster to wrote the test by yourself using the webdriver.

There are some annoying things that you need to pay attention to them when you automate web flow.I will try to cover some of them in another post.

אין תגובות:

הוסף רשומת תגובה