[JAVA] I tried to use Selenium like JQuery

Selenium is very useful for automated testing of web systems, It takes a long time to write test code manually Wait for it to be clickable, click with action, etc. I can't operate it as if I didn't get the hang of it, and it's quite difficult to get to it at first.

For example, a code that just clicks on an element would look like this:

WebElement element = driver.findElement(By.id("id"));
wait.until(ExpectedConditions.elementToBeClickable(element));
Actions actions = new Actions(driver);
actions.click(element);

I tried to write this like JQuery as follows.

$("#id").click();

Since I also implemented find and children, I can write the code to get the number of rows in a table as follows.

$("#id").find("TBODY").children("TR").size();

If this is the case, anyone who can use JQuery can write it without knowing selenium.

To use it, just drop the following source on github and prepare the following method on the test class side. (Keep the driver as a class variable)

https://github.com/widora99/seleniumTest/blob/master/ExtendElement.java

public static ExtendElement $(String selector) {
	return new ExtendElement(driver, driver.findElement(By.cssSelector(selector)));
}

If you want to get multiple elements, please also create the following method. (Due to Java restrictions, it is necessary to specify the return type, so there is no choice but to use another method.)

public static List<ExtendElement> $$(String selector) {
	ExtendElement ele = new ExtendElement(driver, driver.findElement(By.tagName("body")));
	return ele.finds(selector);
}

ExtendElement is a class that extends WebElement, Instead of implementing WebElements to make it look like JQuery The method is defined with its own name. For example, getText () becomes text (). doubleClick () is dblclick ().

Children had the hardest time. In Selenium's cssSelector, specifying "> *" will result in an error, so I have no choice but to convert it to xPath and get it.

public List<ExtendElement> children(String selector) {
	List<ExtendElement> elist = new ArrayList<ExtendElement>();
	String xpath = cssToXpath(selector);
	
	List<WebElement> wlist = elm.findElements(By.xpath("./" + xpath));
	for(WebElement welm : wlist) {
		elist.add(new ExtendElement(driver, welm));
	}
	return elist;
}

However, since the class name is compared by String, "img2" will hit by specifying "img", There is a problem that multiple class names cannot be specified. (Same for closest)

I couldn't do it with my own knowledge, so If anyone knows a good way, I would appreciate it if you could comment or pull.

Recommended Posts

I tried to use Selenium like JQuery
I tried to verify yum-cron
I tried to implement the like function by asynchronous communication
[Rails] I tried to implement "Like function" using rails and js
I want to use FireBase to display a timeline like Twitter
I tried to chew C # (indexer)
I want to use FormObject well
I tried to summarize iOS 14 support
I tried to interact with Java
I tried to explain the method
I tried to summarize Java 8 now
I tried to chew C # (polymorphism: polymorphism)
I tried to explain Active Hash
I tried to implement Ajax processing of like function in Rails
I tried to write code like a type declaration in Ruby
I tried setting Java beginners to use shortcut keys in eclipse
I tried to summarize the methods used
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
I want to use DBViewer with Eclipse 2018-12! !!
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to build AdoptOpenjdk 11 on CentOS 7
What is Docker? I tried to summarize
I tried to build Ruby 3.0.0 from source
I want to use @Autowired in Servlet
I tried to touch JavaScript Part.2 Object-oriented
I tried to implement ModanShogi with Kinx
I tried to make full use of the CPU core in Ruby
I tried to summarize about JVM / garbage collection
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to implement polymorphic related in Nogizaka.
I tried to manage struts configuration with Coggle
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to manage login information with JMX
I tried to organize the session in Rails
I want to use arrow notation in Ruby
java I tried to break a simple block
I tried to link grafana and postgres [docker-compose]
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
I tried to chew C # (basic of encapsulation)
I tried to implement deep learning in Java
I want to use java8 forEach with index
[Must see !!!] I tried to summarize object orientation!
I tried to create a LINE clone app
I tried Spring.
I tried to build AdoptOpenJDK 8 (Addition: Amazon Corretto 8)
[Ruby basics] I tried to learn modules (Chapter 1)
I tried tomcat
I tried to output multiplication table in Java
How to use JQuery in js.erb of Rails6
I tried to link JavaFX and Spring Framework.
I tried refactoring ①
I tried to set tomcat to run the Servlet.
I tried to build Micra mackerel in 1 hour!
I tried to develop an application in 2 languages