Friday, 8 July 2016

How to extract digits from a String in Java

Many times, we have to verify the numbers from a given string.

Below Java code will help get the digits from the string.

public String extractDigits(String str){
       return str.replaceAll("[^0-9]","");
    }

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Monday, 4 July 2016

How to auto download a file in Selenium

When you try to download a file from a website in Selenium, window dialog may appear. But Selenium can not handle such native windows. We may use AUTOIT scripts for handling native windows. But that's only possible on Windows OS.

Better solution is to change the driver settings so that file is automatically downloaded to system bypassing the File save dialog.

If you are using Firefox, you can use below code to start the driver with below profile.

FirefoxProfile myprofile= new FirefoxProfile();
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.dir", "c:\\mydownloadlocation\\xyz");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
WebDriver driver = new FirefoxDriver(myprofile);

If you are using Chrome driver, you can use below code to skip the download window.

        ChromeOptions options = new ChromeOptions();
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("download.prompt_for_download", false);
        prefs.put("download.default_directory", "path-to-download-directory");
        options.setExperimentalOption("prefs", prefs);
        WebDriver driver = new ChromeDriver(options);

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

How to find total number of rows and columns in a table in Selenium

We can use below XPATH expressions to find count of rows and columns in a table.

  1. //table//th  - Find total number of columns in a table. Note that this will only work if all columns are marked using th tag
  2. //table//tr - Find total number of rows in a table.
  3. //table//tr[1]//td - Find total number of TD tags inside a given table row. 

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

How to verify if text is present on a web page in Selenium

We can use below xpath selector to verify if the text is present on a web page in Selenium.

//*[contains(text(),'text data')]

Note that above xpath will identify text wrapped in any element like div, span etc.
Only condition is that text should be inside one specific tag.


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

How to clear data from a text box using JavaScriptExecutor in Selenium

Sometimes, clear method does not work. It does not clear the data from text box. In those situations, we can use Java Script to clear the data.
Below line clears the data from edit box.

((JavascriptExecutor) driver).executeScript("arguments[0].value ='';", element);

Note that second parameter is the name of Web Element (Text box).

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Saturday, 2 July 2016

Selenium IDE Add-On for Firefox

Selenium IDE is a Firefox Add-On that can be used to record and playback the Selenium scripts. We can also export the scripts to various programming languages like C#, Java, Ruby and Python.

You can download the Add-On XPI file from the link - https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/

Next you can install the Add-On in Firefox. Once installation is successful you will notice the Selenium IDE menu in Tools as shown in below image.

Selenium IDE menu

Below image shows the Selenium IDE GUI. We can record the script by clicking on red circle icon.


Below image shows that we can export the recorded script to Ruby, Python, Java, C# etc.




What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Limitations of Selenium WebDriver

Even though Selenium is the most popular automation testing tool, it has got below limitations.

  1. We can only test web applications. We can not test desktop based applications. 
  2. We can test mobile applications but we need to use the Appium for that.
  3. We can not automate the CAPTCHA 
  4. We can not automate the complex control like Flash, Silver light Components, Applet controls, native window dialogs etc
  5. We often encounter issues like Selenium not launching latest browsers due to incompatibility issues. In such scenarios, we have to downgrade the browser in order to automate it.


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Selecting drop down value using JavaScript in Selenium

In some web applications, when we select a value from the drop down, new controls or elements appear or disappear on the webpage. In short, change event is fired. But sometimes, change event does not get fired after selecting the value from drop down using selenium.

In such scenarios, we can use JavascriptExecutor to fire the change event on drop down element as shown in below code.

protected void fireChangeEvent(WebElement element){
        ((JavascriptExecutor) driver).executeScript(" var evt = 
document.createEvent('HTMLEvents'); evt.initEvent('change',true,true); 
arguments[0].dispatchEvent(evt);",element);
    }

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Enter data in text box using JavaScript in Selenium

Sometimes, sendKeys method does not work on text boxes. In such scenarios, you can use JavascriptExecutor to set the value in edit box. Below method takes 2 arguments. It sets the value passed in second argument in text box identified as first parameter.

protected void setValueByJavaScript(WebElement element,String value) {
        ((JavascriptExecutor) driver).executeScript("arguments[0].value ='"+
value+"';", element);
    }


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Setting up Selenium and Maven project

In Maven project, you can add below dependency and start writing the tests in JUnit or TestNG framework.

<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.53.1</version>
</dependency>

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Buy Best Selenium Books