Showing posts with label Selenium test. Show all posts
Showing posts with label Selenium test. Show all posts

Monday, 26 June 2017

This week 12/2017

In this post I'd like to present handy pattern which is used to selenium test called "PageObject". Truly, it is a facade pattern which hides technical aspects of html and javascript content and shares only business friendly API.
Example PageObject bellow:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
@PageObject
public class PtfPageObject {

    private static final String MENU_SECTION = "//table[@ng-controller='AppController']";

    @FindBy(xpath = MENU_SECTION + "//tr[@id='summarize']/td[8]/span")
    private WebElement profitAmount;

    @Autowired
    private WebDriver webDriver;

    public String findProfitAmount(){
        TimeoutUtils.waitForAngularJS(webDriver);
        return profitAmount.getText();
    }
}

I have turned this topic up after a half year. That time I used it to test web pages supported by JSP, ZK Framework or AngularJS pages and I found solution to synchronize an asynchronously loaded content.
Solution for AngularJS bellow:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private static final int ANGULARJS_TIMEOUT = 30;
private static final int WAIT_FOR_NEXT_CHECK = 1;
public static final Function<JavascriptExecutor, Boolean> angularJSInProgress = (JavascriptExecutor drv) -> {
    String ngFinishedAllRequests = "var pendingRequests = angular.element(document.body).injector().get('$http').pendingRequests;"
            + " return (pendingRequests.length === 0)";
    return (boolean) drv.executeScript(ngFinishedAllRequests);
};

public static void waitForAngularJS(WebDriver webDriver, int secondTimeout) {
    Stopwatch stopwatch = Stopwatch.createStarted();
    boolean inProgress = angularJSInProgress.apply((JavascriptExecutor) webDriver);
    while (!inProgress) {
        inProgress = angularJSInProgress.apply((JavascriptExecutor) webDriver);
        if(stopwatch.elapsed(TimeUnit.SECONDS) > secondTimeout){
            throw new TimeoutException("Timeout occured - " + stopwatch.elapsed(TimeUnit.SECONDS));
        }
        try {
            TimeUnit.SECONDS.sleep(WAIT_FOR_NEXT_CHECK);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

You can use the method waitForAngularJS to wait for a response from server and do not try to guess how long it can take.
In the same way I resolved synchronization with VueJS.
I didn't try to resolve connection state in case of jquery or native request in a different way than add counter of begun connections. Maybe such information is available somewhere in browser driver.

Tuesday, 19 April 2016

This week 3/2016

It is my first meeting with Selenium. I didn't know how to start, finally I watched and read a few tutorials. Than I installed the Selenium plugin for Firefox. I was looking for a plugin for Chrome but one plugin I found isn't so useful as this in Firefox.
Than I recorded my first test and exported to a Java class. Hm.. I found six different options of exporting my record to Java code. I can choose one of a few frameworks and type of testing path - local or remote. The plugin allow to  generate Java code dedicated for old Selenium libraries bellow 3 version (it is deprecated code in version 2.52) and developing type for future 3rd version of  Selenium libraries (WebDriver).

I tried all possible combinations except dedicated for TestNG framework.
Finally I can compare some common functionality. Below I  present main difference.
Old SeleniumWebDriver
Selenium interface is deprecated and will be removed in 3.x version
Supports regular expressions in disabled native mode/td>Till now I didn't find any way to localise elements by regular expressions by xpath
All actions are accessible by one interfaceFunctionality of old interface is available by a few separated classes


Next my question was, how can I execute a test generated by IDE. Is it possible to run it from maven and what I need to run.
The IDE generated me a simple test class, in which I updated example package path. Class can be executed as unit test. However it is required to have run test environment for pass a test. I found no other way to do it.