Selenium can test your web applications quite easily (at least the frontend). To perform the tests it launches a webdriver which in most cases will be Firefox. But what about other browsers that may interpret CSS/JS differently, or what about mobile browsers on mobile devices? Selenium does support them, but to get them working some extra configuration is required. In this article I'll show how to make other browsers work with Python Selenium client (on Linux).

We will start with an Selenium code that opens google.com and takes a screenshot of the page. By default if you have Firefox in the system it will (should) work out of the box:
from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://www.google.com')
browser.save_screenshot('screen.png')
browser.quit()

Android

To run Selenium on an Android device you will have to install Selenium server APK. To run it on an emulator from the SDK you will have to install it via adb.

Download android-server-*.apk and install it on the device (either download it on the device and run downloaded file or copy it to the device when connected to the computer).

For emulators managed by the Android SDK you will have to use platform-tools/adb to do the job. First list available devices/emulated systems:

./adb devices
Copy the ID of the device/emulated device you want to use and install the APK on it:
./adb -s ID_NUMBER -e install -r android-server-*.apk
Now start the application. For emulated devices and real devices connected to your computer you need to set port forwarding:
./adb -s ID_NUMBER forward tcp:8080 tcp:8080

This will allow Selenium to communicate with the server. For real devices connected to the same network you can try the device IP without port forwarding.

Page screenshot made by Selenium on Android
Now for the Selenium client configuration. We will have to use remote webdriver - webdriver.Remote:
from selenium import webdriver

desired_capabilities = webdriver.DesiredCapabilities.ANDROID
desired_capabilities['deviceOrientation'] = 'landscape' #portrait

browser = webdriver.Remote("http://localhost:8080/wd/hub", desired_capabilities=desired_capabilities)
browser.get('http://www.google.com')
browser.save_screenshot('screen_android.png')
browser.quit()

Chromium and Chrome

On Linux Chromium is common, but you may also install Chrome. In case of Ubuntu and alike you will find chromium-chromedriver in the repository. When installed you can pass the path to the webdriver in the script to use Chromium:
from selenium import webdriver

browser = webdriver.Chrome(executable_path='/usr/lib/chromium-browser/chromedriver')
browser.get('http://www.google.com/')
browser.save_screenshot('screen_chromium.png')
browser.quit()
For Chrome download chromedriver and set path to the extracted webdriver file.

Opera

There is a Opera webdriver, but I couldn't make it run on my Xubuntu box. The recipe is as follow. Download selenium-server-standalone-*.jar and set a path to this file under SELENIUM_SERVER_JAR variable in shell:

export SELENIUM_SERVER_JAR=.../selenium-server-standalone-*.jar
Next the Selenium code:
from selenium import webdriver

browser = webdriver.Opera()
browser.get('http://www.google.com')
browser.save_screenshot('screen_opera.png')
browser.quit()
You may need to install PhantomJS if it will fail due to missing PhantomJS-related things. As the documentation states it also should support mobile Opera. I got some Java exceptions and thats it.

iOS - iPad, iPod, iPhone

iOS is only supported on OSX (even if you want to use a real device). If you have it, check the ios webdriver page.

If you don't want to play with OSX or Xcode and Apple developers accounts you can try (payed) services that provide multiple browsers and systems for Selenium - like saucelabs.com or testingbot.com

blog comments powered by Disqus

Categories