Wednesday, December 28, 2011

Selenium Grid 2 - Up and Running

WebDriver and Grid 2 WebDriver is the latest greatest API for testing browsers, and is incorporated into Selenium 2 in a variety of languages, including Python. I'm building it into my ptest framework as well, in the WebDriverLib class in the weblib.py module.

The plan is to move all the legacy Selenium 1 tests to Grid 2

Since all of my tests are in Selenium 1 and WebDriver is a completely different API (woo hoo) I have a translation layer to map the existing tests to the new API.

cd $TEST_HOME/src
python
   then, in the python shell ...

 from weblib import WebDriverLib settings = {"browser": "*iehta"}
lib = WebDriverLib(settings)
email = settings.get('email', 'auto_test@your.co.com')
password = settings.get('password', 'autotest1')
dev_url = settings.get("dev_url", "https://your.co.com")
lib.open(dev_url) # translation layer for 'get'
lib.wait_and_type("//input[@id='Email']", email) # translation layer for find element and send_keys
lib.wait_and_type("//input[@id='Password']", password)
lib.wait_and_click("//a[@class='ui-button submit']") # translation layer for find element and click it
lib.wait_for_page_to_load(30000)

 Grid 2
BASICS:
 to launch the hub locally:
    java -jar selenium-server-standalone-2.15.0.jar -role hub

to launch a node (formerly 'remote control') locally:
    java -jar selenium-server-standalone-2.15.0.jar -role node -port 5555 -hubHost localhost -hubPort 4444 -hub http://localhost:4444/grid/register

 running on my qa-jenkins machine at port 4445:
hub:
     nohup java -jar selenium-server-standalone-2.15.0.jar -role hub -port 4445 node: java -jar selenium-server-standalone-2.15.0.jar -role node -port 5555 -hubHost qa-jenkins -hubPort 4445 -hub http://qa-jenkins:4445/grid/register node

multiple firefox nodes on the remote control machine (note the different versions and executable paths specified)
     java -jar selenium-server-standalone-2.15.0.jar -role node -port 2333 -hubHost qa-jenkins -hubPort 4445 -hub http:// qa-jenkins :4445/grid/register -browser browserName=firefox,version=3.6,firefox_binary=c:\progra~2\mozill~2\firefox.exe,maxInstances=1,platform=WINDOWS
and
    java -jar selenium-server-standalone-2.15.0.jar -role node -port 2888 -hubHost  qa-jenkins  -hubPort 4445 -hub http:// qa-jenkins :4445/grid/register -browser browserName=firefox,version=8,firefox_binary=c:\progra~2\mozill~1\firefox.exe,maxInstances=1,platform=WINDOWS

for ptest config:
 -b *firefox3 will invoke the 3.6 node, and *firefox8 will invoke the 8 node ptest
example:
 python ptest.py -c webtest_config.json -t test_webdriver -w true -b *firefox8

 Internet Explorer node:
     java -jar selenium-server-standalone-2.15.0.jar -role node -hub http://qa-jenkins:4445/grid/register -browser browserName=iexplore,version=9,platform=WINDOWS -port 8000

 Google Chrome node: (note the path is to the chromedriver executable, not to an installed Chrome browser. Also, the path to the chromedriver executable must be set in the PATH environment variable)
 java -jar selenium-server-standalone-2.15.0.jar -role node -port 6000 -hubHost qa-jenkins -hubPort 4445 -hub http://qa-jenkins:4445/grid/register -browser browserName=chrome,chrome_binary=C:\chromedriver\chromedriver,maxInstances=1,platform=WINDOWS

 see http://code.google.com/p/selenium/wiki/ChromeDriver for more information

For SSL Issues with Google Chrome, initialize the WebDriver client with a chrome.switches array as below (python bindings example) ->

                    from selenium import webdriver

                    dc = webdriver.DesiredCapabilities.CHROME
                    dc["chrome.switches"] = ["--ignore-certificate-errors"]
                    driver = webdriver.Remote(str(remote_url), self.dc)

Thursday, December 8, 2011

Selenium and SSL

If you have to test https sites using Selenium on a variety of browsers, you are asking for a world of pain!  Practically every version of every browser on every platform requires either a different jar file or different arguments for both the selenium server and the remote client.

First of all, the all-important version disclaimer!
   selenium-server-standalone-2.15.0.jar
   selenium-grid-1.0.8
   Firefox 3.6  on Windows 7 and Ubuntu 11.04
   Firefox 8 on Windows7
   Chrome 15.0.874.121 on Windows 7
   Internet Explorer 9 on Windows 7

For Local Selenium Server
-------------------------------------
 java -jar selenium-server-standalone-2.15.0.jar


For Selenium RC Clients (your test app)
-----------------------------------------------------
  call selenium.start with the commandLineFlag -disable-web-security

  Python example:
    from selenium import selenium
    sel = selenium(test_host, int(test_port), self.browser, url)
    sel.start('commandLineFlags=-disable-web-security')

For Selenium Servers and Selenium Grid Remote Clients
---------------------------------------------------------------------------

For Firefox, I needed to use a profile and pass it into the startup script.
  1. start firefox from the command-line with "firefox -profileManager" and create a new profile
  2. Manually go into the site using Firefox, accept the various certificate challenges, and then quit the browser
  2. Start the selenium client using your new firefox profile. For example, as a Selenium Grid remote client:
           ant -Dport=5777 -Denvironment="*chrome" -Dhost=MY_IP
 -DhubURL=http://SELENIUM_GRID_SERVER_IP:4444 -DseleniumArgs="-firefoxProfileTemplate C:\Users\ME\FIREFOX_PROFILE_DIRECTORY_COPY\firefox_profile"  launch-remote-control

you may need to add some lines to the prefs.js file in the firefox profile. for example, if you see 403's and/or 404's being returned because of the browser looking for /favico.ico, you should add these two lines
    user_pref("browser.chrome.favicons", false);
    user_pref("browser.chrome.site_icons", false);
NOTE that this USUALLY WORKS but, in my case, it simply STOPPED WORKING on Windows 7. The -firefoxProfileTemplate argument passed in to ant as seleniumArgs is not passed along by the remote control to the firefox startup command. Here the specified profile is simply ignored:
    [java] 09:16:47.113 INFO - Preparing Firefox profile...
     [java] 09:16:49.072 INFO - Launching Firefox...
     [java] 09:16:49.074 DEBUG - Execute:Java13CommandLauncher: Executing 'C:\Program Files
(x86)\Mozilla Firefox\firefox.exe' with arguments:
     [java] '-profile'
     [java] 'C:\Users\ME\AppData\Local\Temp\customProfileDirc829f057ff9e497ea065add1ca892726'


The solution was to replace the selenium-server-standalone jar file in selenium-grid-XX/vendor with a newer one from Selenium org (2.15) - However, this broke IE which needed selenium server standaloine 2.12.0 !!!)


For Internet Explorer
    Disable popup blockers - Select Tools/Popup Blocker/Turn off pop-up blocker
    Disable IE protected mode - Untick Tools/Internet Options/Security/Enable protected mode - do this for all four zones

For Google Chrome
    Open Chrome Options
    go to 'Under the Hood'
    click on the 'Manage Certificates' button at HTTPS/SSL
    IMPORT your https server's PFX certificate and save it under Trusted Root Certification Authorities (input the password when prompted)