Saturday, September 22, 2012

Dealing with web elements by different locators

Let’s consider the following sample HTML code snippet as a reference point.
  1. <!DOCTYPE HTML>
  2. <html>
  3.     <head>
  4.         <script type="text/javascript">
  5.             function jsFunction(){
  6.                 alert("Welcome to Selenium UI Automation Testing !");
  7.             }
  8.         </script>
  9.     </head>
  10.     <body>
  11.         <input type="submit" id="webButton" onclick="jsFunction()" value="Click It" />
  12.     <a href="http://aksahu.blogspot.in/">Go Home</a>
  13.     </body>
  14. </html>
Lets start by taking example of any of the selenium command say "click"
You can use same fashion for any of the selenium command like i am using for click.
The click command emulates a click operation for a link, button, checkbox or radio button. It takes a locator (an identifier for which HTML element the command refers to) as an argument.

Using Selectors with Click Command:

xpath

This allows clicking on an element using an XPath expression. Example

selenium.click("xpath=//input[@name=webButton' and @type='submit']");

css

CSS locator is used with Selenium commands to uniquely identify an object or element on a web page. Example,

selenium.click("css=input[name=webButton]”);

name

The name selector is used to click the first element with the specified @name attribute. Example,

selenium.click("name=webButton”);
or
selenium.click("webButton");

id

This allows click on an element with the specified @id attribute. Example,

selenium.click("id=webButton”);

link

This allows clicking on a link element which contains text matching the specified pattern. Example,

selenium.click("link=Go Home”);


Check if selenium server is running in background

When we run selenium RC test cases sometimes we faces this issue saying "java.net.BindException: Selenium is already running on port 4444. Or some other service is.
When you check the port 4444 no service is running. We change the port and run the program even that too is not working. 
In these cases we need to shutdown the selenium server on this port.
Use below command to shut down the server.

http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer

If selenium server is already running on port 4444 then it will shut down the server and says
OKOK
if selenium is not running on this port 4444 then by hitting above url will give you 
"Unable to connect"
Now you can run your test cases i am sure will run smoothing.

You can also do an alternative way to handle this i.e you can and some code to check whether the selenium server in a specific port is running or not before starting the server like below:

//ServerControl.java
  1. package test.example;
  2. import java.net.HttpURLConnection;
  3. import java.net.URL;
  4. import org.openqa.selenium.server.RemoteControlConfiguration;
  5. import org.openqa.selenium.server.SeleniumServer;
  6. import org.testng.annotations.AfterClass;
  7. import org.testng.annotations.AfterSuite;
  8. import org.testng.annotations.BeforeClass;
  9. import org.testng.annotations.BeforeSuite;
  10. import com.thoughtworks.selenium.DefaultSelenium;
  11. import com.thoughtworks.selenium.Selenium;
  12. /**
  13.  * This class contains methods to control selenium server
  14.  *
  15.  * @author aksahu
  16.  */
  17. public class ServerControl {
  18.     protected Selenium selenium;
  19.     protected SeleniumServer seleniumServer;
  20.     private static String host = "localhost";
  21.     private static int port = 4444;
  22.     @BeforeSuite
  23.     public void startSeleniumServer() {
  24.         if (!isSeleniumServerRunning()) {
  25.             try {
  26.                 RemoteControlConfiguration rc = new RemoteControlConfiguration();
  27.                 seleniumServer = new SeleniumServer(rc);
  28.                 seleniumServer.start();
  29.             } catch (Exception e) {
  30.                 e.printStackTrace();
  31.             }
  32.         }
  33.     }
  34.     @BeforeClass
  35.     public void startSelenium() {
  36.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  37.         selenium.start();
  38.         selenium.windowMaximize();
  39.     }
  40.    
  41.     @AfterClass
  42.     public void stopSelenium() {
  43.         selenium.stop();
  44.     }
  45.     @AfterSuite
  46.     public void stopSeleniumServer() throws Exception {
  47.         seleniumServer.stop();
  48.     }
  49.     /**
  50.      * Check whether the selenium server in the given port is running or not
  51.      * @return
  52.      */
  53.     public static boolean isSeleniumServerRunning() {
  54.         try {
  55.             String baseUrl = "http://" + host + ":" + port;
  56.             System.out.println("Checking selenium server status [" + baseUrl + "]");
  57.             URL url = new URL(baseUrl + "/selenium-server/driver/?cmd=testComplete");
  58.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  59.             if (connection.getResponseCode() == HttpURLConnection.HTTP_OK)
  60.                 return true;
  61.         } catch (Exception e) {
  62.             System.err.println("Could not check selenium server status: " + e.getMessage());
  63.         }
  64.         return false;
  65.     }
  66. }

In the above the method isSeleniumServerRunning() returns a boolean value true if the server is running in the given port and host and returns false otherwise.