Pages

Thursday, March 6, 2014

Selenium in 3 Days – Day 3 – Be an expert in Selenium Robot Framework.

Today, the last day of your selenium training, I will start with four FAQs. 
  
      Q.   Can I change the location where the log and result files create in?
A.    Yes, you can. you can give a directory where the logs and result files stored in. It is not mandatory to exist the directory. Here, ‘<foldername>’.
>pybot --outputdir <foldername> <test_suit_name>


      Q.     Can I execute selected test cases from my test suite?
      A.      Of course you can! Suppose you have a test case named ‘testcase001’ in your test suit. You can execute only that particular test case using below command.
>pybot --test testcase001 <test_suit_name>
Further, you can check the usage of pybot with below command.
>pybot --help

      Q.     How can I find the locator of a web element?
      A.      You can locate elements by several ways. by id, by name, by xpath, by link text, by partial link text, by tag name, by class name, by css selector.. etc. but with my experience, best strategy to locate an element is 'xpath'. If you are using firefox, you are having the firebug which is the best tool to find locators.  Just press 'F12' in your keyboard. Basic HTML knowledge would be sufficient to find the html code line of your web component. Just right click on it and choose 'Copy Xpath'. Or else, you can use the firefox addon – “element locator for web driver”.  Also you can use the addon – “firepath” to customize xpaths.
You can find basic xpath syntaxes from here-http://www.w3schools.com/xpath/xpath_syntax.asp

      Q.     If my selenium script running in a windows PC, can I schedule it to automate?
      A.      Yes, you can save your pybot command as a executable .bat file and schedule it with the windows scheduler.

From here, I’m going to talk about two major topics.

Working with windows dialog box

When you working with web applications, you may have to download several objects in a web page. (example – text/csv files, excel/word documents, zip files, jar files… etc ), in that case firefox will ask you to confirm before download. 
Look. This dialog box is NOT a component of the browser. It’s a windows dialog box. You neither can handle it through selenium2 library nor builtin library. So, how can we overcome this?
You have two major options to do this.




  • With the help of AutoIt library.
    • Download AutoIt library from – https://code.google.com/p/robotframework-autoitlibrary/downloads/list
    • Refer ‘README.txt’ for installation instructions.
    • After successful installation, you can see a folder named ‘AutoItLibrary’ inside <python directory>\Lib\site-packages
    • Import the library inside the resource file. (Library     AutoItLibrary)
    • Send simulated keystrokes to the active window using ‘Send’ keyword.
      • Example 1–  Send   !s   for send Alt+s and choose ‘Save File’ option.
      • Example 2 – Send   {ENTER} for send Enter key.
But, wait! There is a limitation on this. You CANNOT use AutoIt library in linux systems. So we have to move to another option.
  • By changing the firefox profile attributes.
This is the best option to work with windows dialog box. The default firefox profile which is using by selenium2library is locating at ‘<python_installation_dir>\Lib\site-packages\Selenium2Library\resources\firefoxprofile’. Just open the ‘prefs.js’ file and change below firefox profile configurations.

firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir","c:\\downloads");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");

Note 1- Value of ‘browser.helperApps.neverAsk.saveToDisk’ should be change according to the document type which you suppose to download.
Example – for zip files ‘application/zip’, for jar files ‘application/java-archive’

Note 2 – Instead of changing default FF profile, you can create a separate FF profile. Refer ‘https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles’ for more information. In this case, you need to mention ‘ff_profile_dir’ with the ‘open browser’ keyword.


Working with databases.

If you are a selenium developer, you may need to connect to a database and verify data which you submit through the web page. How can you do that? I’ll explain it using an example.

How to connect an oracle DB using selenium web driver? 
There are 4 easy steps to do that.

Step 1 : Download and install robotframework-databaselibrary (http://franz-see.github.io/Robotframework-Database-Library/)

Step 2 : Install oracle client, make TNS at tnsnames.ora (Location- <downloaded directory>/network/admin/tnsnames.ora)

Example :   BOBA64 =
                        (DESCRIPTION =
                             (ADDRESS_LIST =
                                 (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.101.230)(PORT = 1521)
                                 )
                               )
                                       (CONNECT_DATA = (SID = BOBA))
        )

Step 3 : Download and install DbApiModule – “cx_Oracle” (http://cx-oracle.sourceforge.net/)

Step 4 : Connect to the db using ‘Connect To Database Using Custom Params’ keyword. (refer http://franz-see.github.io/Robotframework-Database-Library/api/0.5/DatabaseLibrary.html)
 Example : Connect To Database Using Custom Params   cx_Oracle     '<username>', <password>', '<tns>'

Today you have exposed to more complex scenarios in selenium robot framework.   If you have further questions, contact me anytime.  I’m always ready to help you. 
For now, keep practice!  That is the only option to be an automation expert! Good luck! 
{ Read More }


Blogger news

Blogroll

What's Hot