Select Page

Category Selected: 2016

52 results Found


People also read

Accessibility Testing

Cypress Accessibility Testing: Tips for Success

Automation Testing

Test Automation Maintenance Costs: Smart Ways to Reduce

Accessibility Testing

Accessibility Testing with Playwright: Expert Guide

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility

New Methods in WebDriver ExpectedConditions Class

In Selenium WebDriver 2.53.0 release, we have new methods addition in ExpectedConditions class. All are really helpful for extensive validations and adding synchronization points in test scripts.

Attribute Validation

wait.until(ExpectedConditions.attributeContains(By.cssSelector("box"),"name","txt"));
wait.until(ExpectedConditions.attributeToBe(By.cssSelector("box"),"name","txt1"));
wait.until(ExpectedConditions.attributeToBeNotEmpty(element,"name"));
  

Waiting for Elements Count

wait.until(ExpectedConditions.numberOfElementsToBe(By.cssSelector("box"),5));
wait.until(ExpectedConditions.numberOfElementsToBeLessThan(By.cssSelector("box"),5));
wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector("box"),5));
  

Waiting for a text in an element

wait.until(ExpectedConditions.textMatches(By.cssSelector("box"),Pattern.compile("(.*)(\\d+)(.*)")));
wait.until(ExpectedConditions.textToBe(By.cssSelector("box"),"Sample"));
  

White Framework Wait Commands

The White framework does not send an action until a window has entered the idle state after the previous action. In that case, White framework waits automatically if a window is busy.

But how to wait for an element or a particular condition? How to avoid Thread.sleep during automation script development? In White, we can write our synchronization points. In this article, we will show you White framework waiting techniques and commands which are being used in all our desktop automation testing projects at Codoid.

Wait Based On HourGlass

Using WaitBasedOnHourGlass property (default value true), you can configure wait based on presence of hour glass.

CoreAppXmlConfiguration.Instance.WaitBasedOnHourGlass = true;
[/code]
WaitTill Method

WaitTill is one of the methods for Window object. You can use this method to wait until the certain condition matches.

Label lbl = window.Get

In the above example, the script waits until the label field visibility=true. The default wait time is 5000 milliseconds.

You can also change the Default wait time by setting BusyTimeout property as shown below.

CoreAppXmlConfiguration.Instance.BusyTimeout = 10000;
[/code]
WaitWhileBusy method
Window window = application.GetWindow("Calculator");
window.WaitWhileBusy();
[/code]

WaitWhileBusy method makes your script wait until the window is busy. Note: You no need to call this method for every action as it is called automatically at the end of every action.

Wait Hook

If you want to write your custom delay and the written custom code needs to be called after every action, then you can use IWaitHook interface as shown below.

public class CustomWait : IWaitHook
{
public CustomWait()
{
CoreAppXmlConfiguration.Instance.AdditionalWaitHook = this;
}

// Implementation of the IWaitHook interface
public void WaitFor(UIItemContainer uiItemContainer)
{
...
}
}
[/code]

Resizing and Positioning Window

Browser window resizing and positioning can be done easily using Selenium WebDriver. Consider we have opened multiple browser windows in same machine for parallel execution; it is very useful to view execution on each window, if it is positioned and re-sized for user view at run-time.

Window Resize

driver.manage().window().setSize(new Dimension(100,100));

//Note: Dimension should be imported using org.openqa.selenium.Dimension
  

 

Window Position

driver.manage().window().setPosition(new Point(100,100));

//Note: Point should be imported using org.openqa.selenium.Point
  

Stopping Selenium Server Using HTTP Client

In this blog article, we show you how to stop Selenium Server using Apache HTTP Client GET method. The Apache HttpClient library simplifies handling HTTP requests. To use this library download the binaries with dependencies from http://hc.apache.org/.

//URL to stop Selenium Server
String strURL="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer";

//HTTP Default client Object
HttpClient client = new DefaultHttpClient();

//HTTP Get Object
HttpGet request = new HttpGet(strURL);

//Executing the request
HttpResponse response = client.execute(request);
  

Read more about Apache HTTP Client

CSS nth-child Selector

CSS nth-child Selector

In CSS Selector, we have a very useful structural pseudo-class selector i.e. ‘nth-child’ selector. ‘nth-child’ can be used to select ordered elements by giving the expression (an+b) or single element with positive integer as an argument. Consider if you want to get odd rows from a HTML table using Selenium WebDriver, your immediate idea will be ‘separating odd rows using FOR loop’. But we can accomplish it using nth-child (2n+1) selector.

nth-child (2n+1) expanded below.
(2*0) + 1=1=1st Row
(2*1) + 1=3=3rd Row
(2*2) + 1=5=5th Row

Selenium WebDriver-Java code below counts no. of odd rows in a table.

int inCount=driver.findElements(By.cssSelector("table[id='tbl1'] tr:nth-child(2n+1)")).size();
  

The below code gets first row from a table.

WebElement firstRow=driver.findElement(By.cssSelector("table[id='tbl1'] tr:nth-child(1)"));
  

Browser Compatibility

IE9+, FF 11.0+ on Win, FF 10.0.2+ on Mac, Safari 5.1, Chrome 18+ on Win and Chrome 17+ Mac.

Useful XPath Expressions

For Selenium beginners, the below listed XPath examples are really helpful to write a relative XPath instead of messing up with an absolute one.

1) Select First & Last Node
  • Selecting first hyper link in a page_xpath=/descendant::a[1]
  • Selecting first link using Position method_xpath=/descendant::a[position()=1]
  • Selecting last link_xpath=/descendant::a[last()]
  • Selecting first link which has innerText as ‘English’:xpath=/descendant::a[text()=’English’][1]
  • Selecting first input tag which has ‘class’ attribute_xpath=/descendant::input[@class][1]
2) Selecting a node using multiple condition
  • Selects input tag using ‘type’ and ‘class’ attributes value_xpath=//input[@type=’text’ and @class=’gbqfif’]
  • Selects a button which has name and class attributes: xpath=//button[@name and @class]
3) Getting parent node from child node
  • Gets the input tag’s parent node using ‘..’ : xpath= //input[@name=’q’]/..
4) Selecting parent node from child node
  • Selects the ‘DIV’ which is having ‘INPUT’ tag: xpath=//div[input]