HowTo: Twister & Selenium

This is a short tutorial about how to run Selenium test cases on Windows platform with Twister framework installed on Linux machine.

Pre-requisites: This tutorial assumes that you installed Twister framework on a Linux machine and you configured a Twister client on the Linux machine.

1. Install Oracle Java 1.7 on Windows machine

2. On Windows platform, install  ActivePython. You can download it from:
http://www.activestate.com/activepython

3. Download Python Selenium library from
https://pypi.python.org/pypi/selenium
Extract the files from archive, go into dist\selenium-2.33.0 directory and run
python setup.py install

4. Install Selenium IDE for web browser
http://docs.seleniumhq.org/projects/ide/
Please make sure the Firefox browser is not version 22 ( < 22 ). Looks like it has some issues with Selenium.
Verify that the path to Firefox executable file is set in %PATH% environment.

5. Copy the Twister directory client/executionprocess into a local directory ( e.g. D:\Temp\Selenium )

6. Copy the Twister common directory ( can be found in Twister repository at root level ) into
client directory ( e.g. copy common D:\Temp\Selenium\executionprocess )

7. Configure IDE Selenium options for Python WebDriver export. In Firefox, select ‘Tools->Selenium IDE’ to open Selenium.
In Selenium IDE, select ‘Options->Options’ to open the configuration window.
Select ‘Formats’ tab.
Select ‘Python 2/unittest/WebDriver’
Make sure the header section contains the following:
 from selenium import webdriver
        from selenium.webdriver.common.by import By
        from selenium.webdriver.common.keys import Keys
        from selenium.webdriver.support.ui import Select
        from selenium.common.exceptions import NoSuchElementException
        import unittest, time, re
        import TscUnitTestLib as unittest

        def createClass():
            test_class = ${className}()
            return test_class

        class ${className}(unittest.TestCase):
            def setUp(self):
                self.driver = webdriver.Firefox()
                self.driver.implicitly_wait(30)
                self.base_url = “${baseURL}”
                self.verificationErrors = []
                self.accept_next_alert = True
    
            def ${methodName}(self):
                ${receiver} = self.driver

Make sure the footer section contains the following:

        def is_element_present(self, how, what):
            try: self.driver.find_element(by=how, value=what)
            except NoSuchElementException, e: return False
            return True
    
        def is_alert_present(self):
            try: self.driver.switch_to_alert()
            except NoAlertPresentException, e: return False
            return True
    
        def close_alert_and_get_its_text(self):
            try:
                alert = self.driver.switch_to_alert()
                alert_text = alert.text
                if self.accept_next_alert:
                    alert.accept()
                else:
                    alert.dismiss()
                return alert_text
            finally: self.accept_next_alert = True
    
        def tearDown(self):
            self.driver.quit()
            self.assertEqual([], self.verificationErrors)

    test = createClass()
    _RESULT = test.main()

Press OK to save the changes.
Please make sure the indentation for header and footer sections is correct.

That should be all for configuration.

Now you can record the Selenium test cases using the Selenium IDE integrated into Firefox.
Using this IDE, you can record Selenium test cases. To record a test cases, open Firefox browser and go to Tools->Selenium IDE.
Selenium IDE will open and you can press the record button and follow your steps.
After recording, the test case must saved and exported as Python WebDriver from Selenium IDE, File->Export section.
The export will generate a python file that needs to be transferred on Twister server machine in the test cases directory defined
for your user ( “TestCase Source Path” field from Twister GUI Configuration->Paths section ).

To execute the recorded test cases, the following steps must be taken:
1. Go to Twister client directory and start the EP on Windows machine. You have to specify username, the server:port
and a name for the windows client.
e.g.:
cd D:\Temp\Selenium\executionprocess
D:\Temp\Selenium\executionprocess>python ExecutionProcess.py -u user -s 192.168.1.1:8000 -e WinEP

2. Open the Twister GUI, using the user specified at EP start
3. Go to Configuration->System Under test and create a new SUT. Set the windows EP for the new SUT.
4. Return to Projects tab and create a new suite for Selenium test cases and select the created SUT for it, from “Run on SUT” list
5. Add the exported test cases in the suite
6. Select suite, press Libraries button and select TscUnitTest library
7. Press Run
8. Optionally, you can save the project file for later usage

The execution should start and the recorded Selenium test cases should be playback on the Windows machine.
The status of every test cases will updated in Twister GUI.

That should be all.

Happy testing !

11 Responses
  • prakash
    Jan 29, 2014

    Hi
    New to twister.
    Is there any mechanism in twister to connect dut by using serail port?
    Thanks

    prakash Jan 29, 2014
    • Bogdan Popescu
      Bogdan Popescu
      Jan 30, 2014

      Hi,

      We do not provide a library to support serial connection.

      Bogdan Popescu Jan 30, 2014
  • prakash
    Jan 30, 2014

    Thanks for your reply,

    Is there anyway to develop( looking for your guidance/pointer) by using pthon lib like pyserial.

    Thanks for your valuable time and your suggestions.

    prakash Jan 30, 2014
  • Louglas
    Feb 28, 2014

    Thanks for the details shared.
    Im looking at options to use the Twister scripts migrated to Selenium. What options are available.

    Louglas Feb 28, 2014
    • Bogdan Popescu
      Bogdan Popescu
      Mar 9, 2014

      Hi,
      Twister can executed Selenium recorded test cases.

      Bogdan Popescu Mar 9, 2014
  • leander
    Jun 5, 2014

    Hi,

    This does not work. The error received “No clients are started.Please start clients to execute tests” .For some reason the server is not able to detect the remote EP.
    Both machines are ping able and reside on the same domain.
    What have i missed?

    Regards,
    Leander

    leander Jun 5, 2014
    • Bogdan Popescu
      Bogdan Popescu
      Jun 10, 2014

      Hi Leander,
      I updated the procedure to be aligned with the latest code changes.

      Regards,
      Bogdan.

      Bogdan Popescu Jun 10, 2014
  • Leander
    Jun 17, 2014

    Hi Bogdan

    Thanks for the Update.

    There are still a few glitches in the process tho.

    1. TscUnitTestLib.py does not get downloaded to the Remote EP , which gives the error ‘ImportError: No module named TscUnitTestLib’

    2. by this ‘ test_class = ${className}() ‘ , you mean the $(classname) would be the actual class name recorded from selenium without the $() right?

    Thanks,
    Leander

    Leander Jun 17, 2014
    • Bogdan Popescu
      Bogdan Popescu
      Jun 18, 2014

      Hi Leander,

      Thanks for spotting this !
      The library TscUnitTest must be selected for suite, I added the information at the end of post.
      For second question, you should keep the round brackets, too. That syntax means that the class constructor is called.
      For instance, if the recorded class name is sel_class, that line becomes test_class = sel_class() in the generated code.

      Best regards,
      Bogdan.

      Bogdan Popescu Jun 18, 2014
  • Leander
    Jun 23, 2014

    Hi Bogdan,

    Adding the library from the UI didnt work , got the same error.
    Attaching runtime logs here. IT seems that only TscCommonLib is being downloaded into the .twistercache

    Downloading library `TscCommonLib.py` …
    … all libraries downloaded.

    ===== ===== ===== ===== =====
    Starting suite `101 – Selenium`
    ===== ===== ===== ===== =====

    <<>>

    Test case exception:
    File “ExecutionProcess.py”, line 975, in tests
    result = current_runner._eval(str_to_execute, globs, args)
    File “C:\Selenium\client\executionprocess\RunnerClasses.py”, line 247, in _eval
    execfile(fpath, globs_copy)
    File “C:\Selenium\client\executionprocess/.twister_cache/SeleniumRemote/Selenium_appd_login.py”, line 10, in
    import TscUnitTestLib as unittest
    ImportError: No module named TscUnitTestLib

    Leander Jun 23, 2014

Leave a Reply