Saturday, December 13, 2014

Run Selenium IDE Script in Different Browsers using WebDriver Playback

Run Selenium IDE Script in Different Browsers using WebDriver Playback


WebDriver Playback is a newly introduced feature in Selenium IDE. It allows users to run Selenium IDE tests in multiple browsers. Thus increasing usefulness of Selenium IDE as test automation tool. This article explains how we can use WebDriver Playback feature and run our Selenium IDE script in different browsers other than Mozilla Firefox.
We will try to run a Selenium IDE test script on Google Chrome browser. Of course, you can run your test on other browsers such as Internet Explorer, Opera, Android, iPhone, iPad etc. For that, you’ll just have to replace the browser name in WebDriver Playback tab. We’ll see this in our example steps. For now, let’s follow below steps to run Selenium IDE script on Google Chrome.
  • Open Selenium IDE.
  • Go to Options–>Options–>WebDriver tab.
  • Check the option to ‘Enable WebDriver Playback’ from opened tab.
  • Provide ‘chrome’ value in text box and click OK button. We can specify browser name of our choice in the text box. Browser names are given in ‘Browser choices’ text displayed below this text box. Your options dialog should look similar to below image after making these changes.


  • Restart Selenium IDE. Restart is required for the changes to take effect.
  • We will need respective browser driver(in our case, Chrome driver) along with Selenium Standalone JAR file. You can download them from here and here. Once you have these files, place both of them in same folder.
  • Now open your Command Prompt and navigate to the directory where driver and JAR files are stored.
  • Run following command from Command Prompt to start Selenium server and Chrome Driver server. Use respective driver name if you are using a different browser.
java -jar selenium-server-standalone-2.43.1.jar -Dwebdriver.chrome.driver=chromedriver.exe

  • Now, open your Selenium IDE script and run it as you usually do.
  • You should see your test script getting executed in Google Chrome browser.
Please note that the browser will remain open after test script is executed. You will have to manually close it.
As seen from the above example, we can use WebDriver Playback to easily run our Selenium IDE tests in browsers other than Firefox. This feature adds more flexibility in Selenium IDE which can be of great use to all Selenium users. We hope you would find the WebDriver Playback feature useful. Let us know in comments how it goes for you.

Tuesday, May 20, 2014

Display Javascript Alert Message Using Selenium Webdriver

Using below method you can display alert messages in selenium webdriver script


public void Show_ScriptMessage_Close(String Message , int time) throws Exception
{
   
    time = time * 1000; // 1 value to 1000
     final ImageIcon icon = new ImageIcon("path to image icon");
      final JOptionPane optionPane = new JOptionPane(Message, JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, icon , new Object[]{}, null);

      final JDialog dialog = new JDialog();
      dialog.setTitle("Selenium Automation Scripting");
     
      dialog.setModal(true);
     
      dialog.setLocationRelativeTo(null);
      dialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width)/2 - dialog.getWidth()/2, (Toolkit.getDefaultToolkit().getScreenSize().height)/2 - dialog.getHeight()/2);
      dialog.setContentPane(optionPane);

      dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
       
   
      dialog.pack();
     
    //create timer to dispose of dialog after 5 seconds
      // 1 sec = 1000
      Timer timer = new Timer(time, new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent ae) {
              dialog.dispose();
          }
      });
      timer.setRepeats(false);//the timer should only go off once

      //start timer to close JDialog as dialog modal we must start the timer before its visible
      timer.start();

      dialog.setVisible(true);
   
}




You can call the above function with your message and time

for e.g.
Show_ScriptMessage_Close("Checking the login functionality" , 5)




Selecting a date from Datepicker using Selenium WebDriver / Javascript code to pass date in datepicker

We can dynamically pass the date in Date picker field instead of selecting the date from datepicker calender.


WebElement date_element = driver.findElement(By.id(locator));
          

((JavascriptExecutor)driver).executeScript("arguments[0].value=arguments[1]", date_element, date);


Initialise the variable date as string and assign the value in required format. 
e.g String date ;
date = "01 May 2014"



How to Highlight Element with Selenium Webdriver using Java Script


We can highlight the Element using below method - 


 for (int i = 0; i < 2; i++) {
       
    WebElement element = driver.findElement(locator);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].setAttribute('style', arguments[1]);",
                element, "color: blue; border: 3px solid red;");
        js.executeScript("arguments[0].setAttribute('style', arguments[1]);",
                element, "");
       
    }





We can also create the function to call the same frequently.

public void highlightElement(WebElement element) {
   
    for (int i = 0; i < 2; i++) {
       
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].setAttribute('style', arguments[1]);",
                element, "color: blue; border: 3px solid red;");
        js.executeScript("arguments[0].setAttribute('style', arguments[1]);",
                element, "");
       
    }

}



We can call above function as - 

WebElement element  = driver.findElement(By.xpath(locator);
 highlightElement(element  );