This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Learning Web UI Automation

Week 2 - Interacting with Elements on the page

David Burns's picture
Thu, 2011-02-03 18:27

Last week we spent some time having a look at how we can find elements on the page using a number of different
techniques. This is extremely important to automating web applications since we can now interact with them in a number
of different ways.

In Selenium we have do-ers and getters. Note that I didn't say setters and getters. Do-ers have the
ability to do something to the page that will then cause the page to do something. Examples of this are clicking,
typing, dragging and dropping and so on. Getters allow us to get information from the site so that we can validate and
verify something is on the page.

Let's start with the getters to make sure that we can get the information off a page.

Getters
The most commonly used getters are get_title, get_value and get_text

get_title()

This command returns the title of the page by looking to see what is in the <title> tags on the page. If we have  a look
at the html below if we used selenium.get_title() we would get 'I <3 ponies'. Selenium will correct the HTML encoding
before returning it to the test.

<html>
    <head>
        <title>I &lt;3 ponies</title>
    </head>

get_value(locator)

This command looks at value of an element. Elements that have a value attribute will return something otherwise we will
get a None type returned. A None type is the same as a Null in most languages. If we have a look at the example below we
can see it in action.

<input type='text' value='over the rainbow' id=rainbow />

If we had the above in a html file it would show a text box with 'over the rainbow' in it. If we did
selenium.get_value('id=rainbow') it would return the text in that box.

get_text(locator)

This command works in a similar way to get_value but works against text elements on the page. Since most of the page is
text we will use this quite a lot. Below is an example.

<p id=redslippers>Dorothy put on the red slippers</p>

If we did selenium.get_text('id=redslippers') it would return us 'Dorothy put on the red slippers'. There can be issues
with using this command. It can return text from nested elements too. Below is an example.

<p id=redslippers>Dorothy put on the red <a href='http://slippers.mozilla.org'>slippers</a></p>.

If we ran the command from above it would return the exact same text. This is intended behaviour but has caught many people out in the past.

Other getters that are popular are get_location() which returns the address of the web application we are testing.
get_html_source() returns the HTML of the page when you call it. This can be useful for debugging issues.

To see an entire list of the getters open a python REPL by typing python in a command prompt or terminal.

from selenium import selenium
dir(selenium)


Have a look at the API calls prefixed with get_

There are commands in there prefixed with is_. These are getters too but return if the "is" statement is true. For
Example :

selenium.is_element_present("id=foo") will return true if it finds an element with the id 'foo' on the pag. These are
helper methods to check items when testing. We will see more of them next week.

Do-ers

This is exciting part of browser automation. We will write some python that will interact with the browser and tell it
to do things that most users would be doing.

Lets start with the basic one of click.

click(locator)

This is probably the most used command in Selenium since we are clicking between elements on the page constantly. This
can be a link, button, radio button, checkbox and many more. The syntax is fairly straight forward in that we do
selenium.click(locator) as we saw in the simple script I showed you last week.

type_keys(locator, text)

This command will send through the text that you want to put on the screen. if we had a text box like below

<input type=text id=searchBox>

and typed into it, we would do selenium.type_keys("id=searchBox", "Google it...") "Google it..." would appear in the
text box.

select(locator, value)

Select boxes help us keep what the user can send us down to a minimum. A good example of this is selecting which country
you are in when filling in a form.

Those are the most widely used commands but other popular commands are open(url), submit('formLocator').

To see an entire list of the getters open a python REPL by typing python in a command prompt or terminal.

from selenium import selenium
dir(selenium)

Have a look at items that are not prefixed with get_, is_, wait_for and select_. Note the _ for the select since that isnt
used against a select on the page.

Task

This weeks tasks are to create your first tests.

1)Have a look at https://litmus.mozilla.org/show_test.cgi?id=15076. This is a manual test case from our test case
manager. Try and create a selenium test that types the addon name into the text box and clicks search. When your test is
transitioning between pages add self.selenium.wait_for_page_to_load("30000"). We will go in depth how wait_for commands work
next week.

Have a look at http://docs.python.org/release/2.6.6/library/unittest.html for the assert methods you can use

use the template below to get your test running


from selenium import selenium
import unittest

class TestAdblocker(unittest.TestCase):

    def setUp(self):
        self.selenium = selenium("localhost", 4444, "*firefox", "http://addons.allizom.org")
        self.selenium.start()
        self.selenium.open("/")
        self.selenium.window_maximize()

    def tearDown(self):
        self.selenium.stop()

    def test_that_searching_for_adblocker_returns_the_result(self):
        # Insert your test here
        pass

When you have finished this please send me the final script as plain text in an email.