Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build an efficient getElement of open source testing framework

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

I haven't written a technology blog for a long time. I've been fooling around for so many days. I've been in a busy state and wasted a lot of time. Calm down today and think about it and continue on the road.

There was a record of how to build a webdriver test environment, some tips, and an example of how to actually start writing an automated test case. But in a real automated testing project, that's not enough. Think about it. If you want to have driver.findElement for every step, it should be enough for you to drink a pot of it, let alone maintenance. At this time, the light on my brain suddenly flashed, it seems that you have some inspiration, those repetitive sentences, can be encapsulated? So let's start our journey of framework design with the findElement approach.

First of all, let's take a look at the native webdriver method to find elements, take id as an example: driver.findElement (By.id ("")), put aside driver for a while, findElement method contains two parts: By and id (attributes of elements) By are native keywords, here we do not move it, we start with id, in fact, not only id, there are many ways to find elements through element attributes, such as: id,name,cssSelector,xpath,linkText,tagName and so on.

Here we can make an article, for example, we instantiate a By locator method, list all the possible element attributes in it, and call different methods according to the element attributes provided by the user. The element attribute consists of two parts: the attribute name, and the attribute value can be separated by':'.

1. If the user's input does not have':', then the By.id () method is called by default

two。 If the user's input contains':', then according to the actual situation, the element user input is separated by': 'and the specific calling method is determined by the string separated to the left of':'. Pass in the actual attribute value through the separated string to the right of':'

Public static By locator (String locator) {

If (! locator.contains (":") {

Return By.id (locator)

} else {

String [] lArr = locator.split (":")

String by = lArr [0]

/ / get the specific element attributes after the colon

String using = locator.substring (by.length () + 1)

If (by.equalsIgnoreCase ("id")) {

Return By.id (using)

} else if (by.equalsIgnoreCase ("name")) {

Return By.name (using)

} else if (by.equalsIgnoreCase ("xpath")) {

Return By.xpath (using)

} else if (by.equalsIgnoreCase ("cssSelector")) {

Return By.cssSelector (using)

} else if (by.equalsIgnoreCase ("linkText")) {

Return By.linkText (using)

} else if (by.equalsIgnoreCase ("partialLinkText")) {

Return By.partialLinkText (using)

} else if (by.equalsIgnoreCase ("tagName")) {

Return By.tagName (using)

} else if (by.equalsIgnoreCase ("className")) {

Return By.className (using)

} else {

Throw new IllegalArgumentException ("Cannot find elements when name text is null.")

}

}

}

3. Define a method for getEelment

Public static WebElement getWebElement (String locator) {

WebElement we

We = driver.findElement (locator (locator))

Return we

}

This is a simple way to find elements, but what are the benefits of doing so?

A) Don't worry too much about the underlying method of finding the element, all I have to do is provide the attribute of the element, which can be id,name....

B) reuse the findElement method without having to write it many times, only define it once, and call it indefinitely

C) it is easier to maintain, do not need to use the code frequently, make mistakes as long as there are few changes, and achieve the separation of properties and methods

D) more scalable

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report