Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Selenium WebDriver for Cross-Browser Testing, Part 2

DZone 's Guide to

Selenium WebDriver for Cross-Browser Testing, Part 2

We wrap up this quick two-part series by looking at the Python code we need to implement the theory we discussed last time.

· Web Dev Zone ·
Free Resource

Welcome back! If you missed Part 1, you can check it out here

Selenium WebDriver in Action

Now that you are aware of the basics of Selenium and Browser Drivers, it is time we have a look at a simple WebDriver script in our Selenium WebDriver tutorial. Before we look at the example, please make sure that you have the programming environment ready, i.e. download the IDE of your choice (we are using the Eclipse IDE, with all the required packages/software downloaded from the Eclipse Marketplace). The Selenium Plugin for Eclipse can be downloaded from here. The sample code has been written in the Python language, but you can write the test code in C#, Java, Perl, Ruby, etc.

Firefox WebDriver Example

Since the test code will be communicating with the browser (Chrome, Firefox, Internet Explorer, etc.), please ensure that the corresponding client library/WebDriver is installed on your machine. Please refer the section 'Browser Drivers' above on how you can download the corresponding WebDriver.

Below is some sample code that uses Selenium and Firefox WebDriver to open a webpage


# Import the necessary modules for development
import time
import unittest
from selenium import webdriver

# Invoke a new Firefox Instance
ff_driver = webdriver.Firefox()

# Blocking wait of 30 seconds in order to locate the element
ff_driver.implicitly_wait(30)
ff_driver.maximize_window()

# Open the required page
ff_driver.get("http://www.lambdatest.com")

# Sleep for 10 seconds in order to see the results
time.sleep(10)

# Close the Browser instance
ff_driver.close()

Let us perform a code walkthrough. Before we perform any action, all the necessary modules are imported using the ‘import’ statement [Lines 2-4]. In the test code, we make use of the Firefox WebDriver since the testing is done against the Firefox Browser [Line 7]. In the code, ff_driver is the WebDriver handle that would be used for performing any interaction with the Firefox browser. In case you do not have the WebDriver installed or you try to use a browser for which there is no support (via WebDriver), it would give the below error:

selenium.common.exceptions.WebDriverException: Message: 'operadriver' executable needs to be in PATH

The error message is displayed when we try to load the WebDriver for Opera (without the WebDriver installed on the machine). In order to get rid of the error, you should install the WebDriver for Opera from here.

Once all the operations on the browser are complete, ff_driver.close() is invoked in order to do the necessary clean up and freeing up resources.

In order to execute the code, you can invoke CTRL+F9 in Eclipse IDE or compile the code using Python command line options:

Selenium

Chrome WebDriver Example

The previous section in our Selenium WebDriver tutorial, we used the Firefox WebDriver. In this section, we look at the changes that are required in case you are using the Chrome browser for testing. You should ensure that the standalone ChromeDriver binary (which is different than the Chrome browser binary) is available in the right path or it is present as a part of the webdriver.chrome.driver environment variable. If you are using Windows, you need to first download the Chrome WebDriver from here. Paste ChromeDriver.exe in the location where you have installed Python (in our case, it was the default installation path i.e. C:\Python27\Scripts). You have to just load the corresponding WebDriver [Line 8] and rest of the code remains the same. Below is the code snippet, with the change (required for Chrome WebDriver) highlighted in a different color:


# Import the necessary modules for development
import time
import unittest
from selenium import webdriver

# Invoke a new Chrome Instance
ff_driver = webdriver.Chrome()

# Blocking wait of 30 seconds in order to locate the element
ff_driver.implicitly_wait(30)
ff_driver.maximize_window()

# Open the required page
ff_driver.get("http://www.lambdatest.com")

# Sleep for 10 seconds in order to see the results
time.sleep(10)

# Close the Browser instance
ff_driver.close()


Internet Explorer WebDriver Example

So far, we have demonstrated cross-browser testing using the Firefox WebDriver and Chrome WebDriver. In this section, we look at the changes that are required in case you are using Internet Explorer for testing. You should download the correct Internet Explorer WebDriver (32 bit/64 bit) from here or here. Paste InternetExplorerDriver.exe in the location where you have installed Python (in our case, it was the default installation path i.e. C:\Python27\Scripts) or any other location of your choice. If you copy the InternetExplorer WebDriver in the path where Python executable is present, you need not mention the ‘absolute path’ while loading the Web Driver [Line 7]. In the other case, you have to specify the absolute path [Line 9]. Below is the code snippet, with the change (required for IE WebDriver) highlighted in a different color:


# Import the necessary modules for development
import time
import unittest
from selenium import webdriver

# Method 1 - Invoke a new IE Instance
ff_driver = webdriver.Ie()
# Method 2 - Invoke a new IE Instance
ff_driver = webdriver.Ie("path\to\IEDriverServer.exe")

# Blocking wait of 30 seconds in order to locate the element
ff_driver.implicitly_wait(30)
ff_driver.maximize_window()

# Open the required page
ff_driver.get("http://www.lambdatest.com")

# Sleep for 10 seconds in order to see the results
time.sleep(10)

# Close the Browser instance
ff_driver.close()

Lastly, we will have a look at another example where we make use of the ActionChains module which is used to automate low-level interactions like mouse movements, button movements, context menu interactions, etc. to ensure a robust UI design. More details about ActionChains can be found here.


# Import the necessary modules for development
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains

# Invoke a new Firefox Instance
ff_driver = webdriver.Firefox()

# Blocking wait of 30 seconds in order to locate the element
ff_driver.implicitly_wait(30)
ff_driver.maximize_window()

# Open the Home Page
ff_driver.get("https://www.lambdatest.com/")

# Look for the Search Element and enter the Search Criteria

search_criteria = ff_driver.find_element_by_link_text("Support")
actionChains = ActionChains(ff_driver)
actionChains.context_click(search_criteria).perform()
time.sleep(5)

# Perform action on the Images Link
ActionChains(ff_driver) \
    .key_down(Keys.CONTROL) \
    .click(search_criteria) \
    .key_up(Keys.CONTROL) \
    .perform()

# Sleep for 10 seconds in order to see the results
time.sleep(10)

# Close the Browser instance
ff_driver.close()

As seen in the above example code, we retain the code for creating the Firefox WebDriver instance [Lines 9-16]. There are a couple of new modules that are imported – ActionChains and Keys. Once the desired webpage is opened by the browser, a search operation is performed by looking at the Link Text (using the find_element_by_link_text method). In our example, the Link Text that is searched on the Web Page is ‘Support’ [Lines 20-22].

Once the search criteria is encountered, a (CTRL + CLICK) operation is performed, thereby opening that page in a ‘New Tab’ [Lines 26~30]. The ActionChains module is used for performing that operation. The output is shown below:

2_ActionChains_WebDriver-Example-Output-1

We have made use of ActionChains since it was apt for the example which we showcased above. You can have a look at the different modules under Selenium WebDriver module (for Python) here. Though we have used the Python language for implementation, you can make use of the programming language (C#, Java, Perl, Ruby, PHP) of your choice.

Topics:
web dev ,cross-browser testing ,selenium tutorial ,python tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}