Locating Web Elements by attribute/wildcard matching

0 votes

Sometimes we don’t have full control over the key/value pairs used in the HTML. For example, if you have to test a COTS (Commercial Of The Shelf) application or if you are tight to framework specific things, such as JSF or ASP.NET. In this case ids, or parts, are generated and tend to be long and brittle as they might contain generated numbers. A few examples:

ASP.NET

<input id="ctl00_ContentPlaceHolder1_CustomUserControl1_Username" type="text" />

If you change the ClientIDMode property of the control to Static then you will see an id with the following value Username

JSP

<input id="j_id819:inputSequencenumber" type="text" />

In these circumstances you might want to use wildcards, so you will end up with less brittle locators.
Wildcards explained

CSS XPath
Equals E[a=v] //E[@a=v]
Contains E[a*=v] //E[contains(@a, ‘v’)]
Starts-with E[aˆ=v] //E[starts-with(@a, ‘v’)]
Ends-with E[a$=v] //E[ends-with(@a, ‘v’)]

You can define the following locators for the mentioned HTML elements:

input[id$='Username']

and

input[id$='Sequencenumber']

 

Examine your knowledge and play the ‘locator game‘.

Locating Web Elements by id

0 votes

The most common attribute key names are class and id, where id has to be unique over the entire HTML page (according to the HTML specification). This locator strategy allows Selenium WebDriver to find that unique element and perform some action against it.

Example HTML:

<input id="username" type="text" />

We can use one of the following locators:

#username

If we want to be more explicit we can write:

input#username

Using the latter we explicitly mention the type of element.

 

Examine your knowledge and play the ‘locator game‘.

Resize browser window

0 votes

As pointed out in previous post (Maximize browser window), some websites implemented responsive design. You might want to execute some tests in smaller browser windows. Selenium WebDriver gives the ability to resize the browser window.

  @Test
  public void resizeWindow() {
    System.setProperty("webdriver.gecko.driver",
        FileUtil.findFileOnPath("mac/geckodriver"));
    final FirefoxDriver driver = new FirefoxDriver();

    driver.manage().window().setSize(new Dimension(400, 750));

    driver.get("http://www.selenium-in-action.io");
  }

See here a list of viewport sizes: http://viewportsizes.com/