org.kquiet:auto-browser

A java library wrapping selenium to help focus on browser automation.

License

License

Categories

Categories

Auto Application Layer Libs Code Generators
GroupId

GroupId

org.kquiet
ArtifactId

ArtifactId

auto-browser
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

org.kquiet:auto-browser
A java library wrapping selenium to help focus on browser automation.
Project URL

Project URL

https://github.com/kquiet/auto-browser
Source Code Management

Source Code Management

https://github.com/kquiet/auto-browser/tree/master

Download auto-browser

How to add to project

<!-- https://jarcasting.com/artifacts/org.kquiet/auto-browser/ -->
<dependency>
    <groupId>org.kquiet</groupId>
    <artifactId>auto-browser</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.kquiet/auto-browser/
implementation 'org.kquiet:auto-browser:1.1.0'
// https://jarcasting.com/artifacts/org.kquiet/auto-browser/
implementation ("org.kquiet:auto-browser:1.1.0")
'org.kquiet:auto-browser:jar:1.1.0'
<dependency org="org.kquiet" name="auto-browser" rev="1.1.0">
  <artifact name="auto-browser" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.kquiet', module='auto-browser', version='1.1.0')
)
libraryDependencies += "org.kquiet" % "auto-browser" % "1.1.0"
[org.kquiet/auto-browser "1.1.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.25
org.seleniumhq.selenium : selenium-java jar 3.141.59

test (5)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.5.2
org.junit.jupiter : junit-jupiter-engine jar 5.5.2
org.takes : takes jar 1.11.4
ch.qos.logback : logback-classic jar 1.2.3
net.logstash.logback : logstash-logback-encoder jar 5.2

Project Modules

There are no modules declared in this project.

Auto-browser Travis CI build status

Auto-browser is a java library which wraps selenium to help focus on constructing and executing scripts of browser actions in applications.

Using selenium, you can manipulate the browser to perform almost any action as you usually do with a mouse and a keyboard, e.g., log in to an e-commerce site and place orders.

However, webdriver is not thread-safe , so there is still a lot of cumbersome work to do when you want to:

  • execute multiple scripts of browser actions concurrently against a webdriver instance without breaking each other
  • avoid blocking the other script of browser actions while performing wait alike actions on a webdriver instance

Auto-browser handles these internally and provides a fluent way to construct a script of browser actions as an object, enabling cooperation of scripts.

Supported browser

Currently only Chrome/Chromium/Firefox are supported because it should be enough to automate most of web pages. The other browsers(except IE) may be supported in the future.

Getting Started

Add below to maven's pom.xml:

<dependency>
  <groupId>org.kquiet</groupId>
  <artifactId>auto-browser</artifactId>
  <version>X.X.X</version>
</dependency>

Java version 1.8+ is required.

Sample Usage

  1. Construct a script of browser actions:
// Search for the link to the source code of ActionComposerBuilder.java, and then click it
ActionComposer actionComposer = new ActionComposerBuilder()
  .prepareActionSequence()
    .getUrl("https://github.com/kquiet/auto-browser/find/master")
    .waitUntil(elementToBeClickable(By.id("tree-finder-field")), 3000)
    .sendKey(By.id("tree-finder-field"), "ActionComposerBuilder")
    .waitUntil(elementToBeClickable(By.xpath("//mark[text()='ActionComposerBuilder']")), 3000)
    .click(By.xpath("//mark[text()='ActionComposerBuilder']"))
    .returnToComposerBuilder()
  .buildBasic().setCloseWindow(false)
  .onFail(ac -> System.out.println("called when an exception is thrown or the script is marked as failed"))
  .onDone(ac -> System.out.println("always called after all browser actions and callbacks"));
  1. Start a browser:
//ActionRunner encapsulates a webdriver instance and synchronizes all browser actions on it
ActionRunner actionRunner = new BasicActionRunner();
  1. Execute the script:
actionRunner.executeComposer(actionComposer);

Remember to download the driver executable and set its path by system property 'webdriver.chrome.driver' or 'webdriver.gecko.driver' in java command to run, e.g., java -Dwebdriver.chrome.driver=<path to chromedriver>.

Full sample code:

import org.kquiet.browser.*;
import org.openqa.selenium.By;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;

public class Sample {    
  public static void main(String args[]) throws Exception{
    try (ActionRunner actionRunner = new BasicActionRunner()) {
      ActionComposer actionComposer = new ActionComposerBuilder()
        .prepareActionSequence()
          .getUrl("https://github.com/kquiet/auto-browser/find/master")
          .waitUntil(elementToBeClickable(By.id("tree-finder-field")), 3000)
          .sendKey(By.id("tree-finder-field"), "ActionComposerBuilder")
          .waitUntil(elementToBeClickable(By.xpath("//mark[text()='ActionComposerBuilder']")), 3000)
          .click(By.xpath("//mark[text()='ActionComposerBuilder']"))
          .returnToComposerBuilder()
        .buildBasic().setCloseWindow(false)
        .onFail(ac -> System.out.println("called when an exception is thrown or is marked as failed"))
        .onDone(ac -> System.out.println("always called after all browser actions and callbacks"));
      actionRunner.executeComposer(actionComposer).get();
    }
  }
}

Please refer to the api doc for details.

Q&A

  1. How to use firefox as the browser instead of chrome?
    => You can use other constructors of BasicActionRunner to specify the browser type.

  2. How to perform conditional browser actions in a script?
    => Method prepareIfThenElse() in ActionComposerBuilder.ActionSequenceBuilder is designed to perform such actions, please use it accordingly. Or use another method custom() which takes a Consumer as its parameter allowing you to script all the customized logic inside.

Versions

Version
1.1.0
1.0.2
1.0.1
1.0.0
0.5.0
0.4.0
0.3.0
0.2.1
0.2.0
0.1.0