solving selenium common issues framework for each client using selenium, ... the disadvantage, of...

11
SOLVING SELENIUM COMMON ISSUES OBJECT RECOGNITION

Upload: phamxuyen

Post on 13-Mar-2018

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: SOLVING SELENIUM COMMON ISSUES framework for each client using Selenium, ... The disadvantage, of course, is that Link Text can only be used to locate links. 1

SOLVING SELENIUMCOMMON ISSUES

OBJECT RECOGNITION

Page 2: SOLVING SELENIUM COMMON ISSUES framework for each client using Selenium, ... The disadvantage, of course, is that Link Text can only be used to locate links. 1

11WWW.XBOSOFT.COM CONTACT US AT [email protected]

F or software projects today, ever-increasing functionality

comes at the cost of software complexity that grows over

time in ways we may not expect. Test automation assists in

ensuring that new — as well as old — functions will perform

as required and expected. At XBOSoft, we create an optimized

automation framework for each client using Selenium,

accounting for each individual client’s software structure and

properties combined with our client’s goals and objectives.

SELENIUM — RECOGNIZING OBJECTS

O ver the last decade at XBOSoft, we have become adept at

using Selenium for many of our clients’ test automation

requirements. We chose an open-source platform not because of

cost, but because of the large number of developers dedicated to

re�ning the software. In this environment, issues with security

and quality surface quickly and are rapidly addressed.

Comparatively, with proprietary software, manufacturers can

delay �xes, sometimes for months or inde�nitely.

Open-source applications can also be tailored in �exible ways as

required by the business, another bene�t not easily enjoyed by

those using proprietary solutions. Finally, an open-source

solution has a supporting development community that serves

as a valuable resource.

Selenium allows developers to write scripts in a number of

popular programming languages. Further, it can be used in a

variety of IDEs and supports testing frameworks such as JUnit

and TestNG. This �exibility allows QA engineers to get up and

running with Selenium quickly without the need to develop

extensive new skill sets.

Selenium can be con�gured using a variety of WebDrivers that

simulate several browser types (Chrome, Firefox, Safari, for

example). It can also be integrated with add-on test

development tools (Maven with Jenkins, for example) to

support continuous integration, customized report generation

and distribution.

WHY SELENIUM?

Page 3: SOLVING SELENIUM COMMON ISSUES framework for each client using Selenium, ... The disadvantage, of course, is that Link Text can only be used to locate links. 1

12WWW.XBOSOFT.COM CONTACT US AT [email protected]

You normally locate an element by ID, className, tag, or CSS amongst various other methods. Even though there are many ways to locate an element, for various reasons sometimes they don’t work. In this situation, you can use XPath to locate elements.1

1 Using XPath Axes to Locate Elements in Selenium WebDriver. (n.d.). Retrieved from https://xbosoft.com/using-xpath-axes-to-locate-elements-in-selenium-webdriver/

USING XPATH AXES TO LOCATEELEMENTS IN SELENIUM WEBDRIVER

O ur experience with Selenium has been a long and

winding road, and our e�orts with Selenium have not

always been easy. While we’ve designed and implemented many

test frameworks that have provided signi�cant improvements in

script-execution speeds and script maintainability, we’ve learned

many lessons that must be kept in mind to ensure that our

Selenium scripts and frameworks will be useful near- and

long-term. The following list highlights some key considerations:

• Optimizing �exibility and e�ciency while integrating

with other key development tools (Jenkins, for example).

• Solving compatibility issues between the Selenium

WebDriver and speci�c browser drivers created by

outside parties (ChromeDriver is maintained by the

Chromium project, for example) that can lead to crashes.

• Locating an element, irrespective of the type, can be

unreliable. We’ve learned many techniques for

identifying elements and objects in the ‘best’ way for a

given context.

• Understanding where Selenium is capable (Web

application testing), where it may not be the ideal

solution (working with Windows controls/�le

operations/dialog-boxes, for example). Mastering how

to work around unusual issues that can occur during

script execution.

Over the years, what we’ve encountered is that while Selenium

may have a very active support community, that does not

guarantee an easy solution will be found. And one of the most

common issues we’ve run into is that of object recognition. This

white paper steps through the reasons for this problem, why it’s

important, and some of the approaches and solutions we have

identi�ed for addressing this challenging issue.

USING SELENIUM

W ith Selenium WebDriver, it is essential to be able to locate elements so that you can operate on them and

execute automation.

Page 4: SOLVING SELENIUM COMMON ISSUES framework for each client using Selenium, ... The disadvantage, of course, is that Link Text can only be used to locate links. 1

13WWW.XBOSOFT.COM CONTACT US AT [email protected]

bank.htm

We will demonstrate an example of how we leverage

XPath in our Selenium automated testing work to locate

Web elements.

As background, an XPath axes de�nes a node-set relative to

the current node. Names of axes include ancestor, descendant,

parent, etc. (See www.w3schools.com/xml/xpath_axes.asp to

learn more).

When using Selenium for automated testing, assume we are

writing Selenium automation scripts for a page, as de�ned by

the following HTML code.

<html> <head> <title>Bank Search Result</title> </head> <body> <table id=”BankList” style=”width:100%; border-collapse:collapse;” border=”1″ > <tr> <th>Action</th> <th>Bank Name</th> </tr> <tr class=”className”> <td class=”cName” align=”center”><a href=”edit.htm”>Edit</></td> <td class=”cName” align=”center”>Bank 1</td> </tr> <tr class=”className”> <td class=”cName” align=”center”><a href=”edit.htm”>Edit</></td> <td class=”cName” align=”center”>Bank 2</td> </tr> <tr class=”className”> <td class=”cName” align=”center”><a href=”edit.htm”>Edit</></td> <td class=”cName” align=”center”>Bank 3</td> </tr> <tr class=”className”> <td class=”cName” align=”center”><a href=”edit.htm”>Edit</></td> <td class=”cName” align=”center”>Bank 4</td> </tr> </table> </body></html>

The page (above) is a search result page listing some bank names. Our next action is to click the “Edit” link for a given Bank Name.

Because the class, href targets and texts of all Edit links are the same, we can’t use common methods to identify them.

Page 5: SOLVING SELENIUM COMMON ISSUES framework for each client using Selenium, ... The disadvantage, of course, is that Link Text can only be used to locate links. 1

14WWW.XBOSOFT.COM CONTACT US AT [email protected]

At the same time, we can see that the search results (the bank names) always change. Therefore, we can’t operate upon the

expected Edit element with �xed-positioned XPath like //td/a[1].

To solve this problem, we can use XPath Axes. For any Bank Name, if we can identify its sibling node, we can operate the sibling’s

anchor (“<a>” element).

The Java method is like this1:

De�nitions:

• //td[contains(text(),'”+bankName+”‘)] – Locates the wanted element whose text contains the bank’s name.

• preceding-sibling:: – The XPath Axes that selects the bank’s sibling before it.

• td/a – Specify the <a> element of the sibling.

public void clickEditForABank(String bankName)

{ driver.findElement(By.xpath(“//td[contains(text(),'”+bankName+”‘)]/preceding-sibling::td/a”)).click();

}

Page 6: SOLVING SELENIUM COMMON ISSUES framework for each client using Selenium, ... The disadvantage, of course, is that Link Text can only be used to locate links. 1

A complete Java sample code is shown below.

Copy the code and you can run from Eclipse directly. Of course, besides “preceding-sibling,” we can use any other axes in the scripts.

One of the goals of automation is to make testing faster and more e�cient. Thus, it is important to account for script maintenance

when designing your scripts. You want to maintain e�ciency and do less work, rather than add work later with scripts that require

excessive maintenance.

When using Selenium for automated testing, we �nd that using XPath Axes takes less e�ort, gets a better result and makes our

automation scripts �exible and maintainable. Its capabilities enable us to locate complex and even dynamic elements while simplify-

ing the overall maintenance e�ort.

15WWW.XBOSOFT.COM CONTACT US AT [email protected]

Package com.blog.tests;

Import java.util.concurrent.TimeUnit;Import org.junit.*;Import org.openqa.selenium.*;Import org.openqa.selenium.firefox.FirefoxDriver;

public class blog { private WebDriver driver; private String baseUrl;

@Before public void setUp() throws Exception { driver = new FirefoxDriver(); baseUrl = “file:///C:/bank.htm”; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); }

@Test public void testBlog() throws Exception { driver.get(baseUrl); clickEditForABank(“Bank 4”); } public void clickEditForABank(String bankName) { driver.findElement(By.xpath(“//td[contains(text(),'”+bankName+”‘)]/preceding-sibling::td/a”)).click(); }

@After public void tearDown() throws Exception { driver.quit(); }}

Page 7: SOLVING SELENIUM COMMON ISSUES framework for each client using Selenium, ... The disadvantage, of course, is that Link Text can only be used to locate links. 1

1. Values must be manually escaped: In practice, you will be building this string out of variables and literals. If the

variables might contain special characters such as quotes, you will need to escape them.

2. Runtime parsing: The XPath expression is parsed at runtime, which takes resources and processing time, as well as

hiding debugging issues.

3. Learning curve and maintenance: XPath represents additional functions and syntax for all developers who maintain

the code to learn.

4. Implementation di�erences: For example, in IE the �rst node is [0], while the W3C standard stipulates [1] is the

�rst node.

5. Version dependencies: You must be sensitive to the version of the component that will be available to your program.2

Therefore, many people use di�erent object recognition methods including ID, Link Text and CSS Selector, which we discuss below.

For example, if the page source code we are testing is like this:

16WWW.XBOSOFT.COM CONTACT US AT [email protected]

W hile XPath’s bene�ts are well recognized, XPath’s weaknesses include the following:

XPATH WEAKNESSES ANDALTERNATIVE METHODS

2 What is XPath and what does it do? Evaluate the strengths ... (n.d.). Retrieved from https://aw674.wordpress.com/2011/01/03/what-is-xpath-and-what-does-it-do-evaluate-the-strengths-and-weaknesses-of-this-concept-with-examples/

<button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba"><span id="gbqfsa">Google Search</span></button>

Page 8: SOLVING SELENIUM COMMON ISSUES framework for each client using Selenium, ... The disadvantage, of course, is that Link Text can only be used to locate links. 1

17WWW.XBOSOFT.COM CONTACT US AT [email protected]

public class SearchButtonById {

public static void main(String[] args){ WebDriver driver = new FirefoxDriver(); driver.get("http://www.forexample.com"); WebElement searchBox = driver.findElement(By.id("gbqfba")); searchBox.click(); }}

public class SearchElementsByLinkText{

public static void main(String[] args){ WebDriver driver = new FirefoxDriver(); driver.get("http://www.forexample.com"); WebElement aboutLink = driver.findElement(By.linkText("About Google")); aboutLink.click(); }}

Let’s step through some alternative object recognition methods.

Using ID

Speed and precision are advantages of using ID. The disadvantage of using ID is that not all elements have an ID attribute. ID is best

used in elements that have data exchange.

Using Link Text

Another method of locating elements is Link Text. See the example below:

The advantage of Link Text is that this is a special way to locate links. The disadvantage, of course, is that Link Text can only be used

to locate links.

Page 9: SOLVING SELENIUM COMMON ISSUES framework for each client using Selenium, ... The disadvantage, of course, is that Link Text can only be used to locate links. 1

18WWW.XBOSOFT.COM CONTACT US AT [email protected]

public class SearchElementsBycssSelector {

public static void main(String[] args){ WebDriver driver = new FirefoxDriver(); driver.get("http://www.forexample.com"); WebElement about cssSelector = driver.findElement(By.cssSelector("button.btn.btn_big.btn_submit")); aboutLink.click(); }}

Using USS Selector

Another method of �nding elements is CSS Selector. See the example below:

The advantage of CSS Selector is it can locate all elements like XPath and is quicker than XPath. But CSS Selector does not support

some convenient logic (previous item, next item, counters, for example) for Internet Explorer. However, it does work for Chrome.

If you cannot use XPath for some reason — among the many alternatives — we think CSS Selector is the next best choice.

S elenium can provide many bene�ts and be a powerful

way for you to conduct test automation for your Web

platforms. It can also be integrated with Appium to extend

your scripts to mobile platforms. However, as you’ve seen,

sometimes it’s not so easy. What we’ve done in this white

paper is discuss one of the most common problems we run

into with Selenium — recognizing elements — along with

some potential solutions and alternatives.

Please feel free to contact us with questions or comments.

SUMMARY

Page 10: SOLVING SELENIUM COMMON ISSUES framework for each client using Selenium, ... The disadvantage, of course, is that Link Text can only be used to locate links. 1

ABOUT XBOSOFT

XBOSoft is a software testing and quality assurance company that o�ers services in web, mobile and desktop applications, as well

as broad domain experience with extended expertise in healthcare, �nance, and software security. XBOSoft’s quality process

assessments and test methodologies speed products to market and improve clients’ software quality and performance throughout

the software’s life cycle.

With a proven track record working for Fortune 100 companies to small independent developers, XBOSoft today has over 100

employees in San Francisco and Beijing o�ces serving clients in the US and Europe. As the �rm surpasses its tenth year of

continuous growth, XBOSoft proudly o�ers its full range of testing and quality assurance for both on and o�-shore services.

PHONE: +1 703 995 9896 EMAIL: [email protected] XBOSOFT

19WWW.XBOSOFT.COM CONTACT US AT [email protected]

Page 11: SOLVING SELENIUM COMMON ISSUES framework for each client using Selenium, ... The disadvantage, of course, is that Link Text can only be used to locate links. 1