[Selenium Java] Bài 23: Viết hàm xử lý chung để dùng lại

Viết hàm xử lý chung để dùng lại

ValidateUIHelpers.class

package anhtester.common.helpers;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

import java.util.List;

public class ValidateUIHelpers {

    private WebDriver driver;
    private WebDriverWait wait;
    private int timeoutWaitForPageLoaded = 30;

    public ValidateUIHelpers(WebDriver _driver){
        driver = _driver;
        wait = new WebDriverWait(driver,10);
    }

    public void setText(By element, String value){
        wait.until(ExpectedConditions.visibilityOfElementLocated(element));
        driver.findElement(element).sendKeys(value);
    }

    public void clickElement(By element){
        wait.until(ExpectedConditions.visibilityOfElementLocated(element));
        driver.findElement(element).click();
    }

    public boolean verifyUrl(String url)
    {
        System.out.println(driver.getCurrentUrl());
        System.out.println(url);

        return driver.getCurrentUrl().contains(url); //True/False
    }

    public boolean verifyElementText(By element, String textValue){
        wait.until(ExpectedConditions.visibilityOfElementLocated(element));
        return driver.findElement(element).getText().equals(textValue); //True/False
    }

    public boolean verifyElementExist(By element){
        //Tạo list lưu tất cả đối tượng WebElement
        List<WebElement> listElement = driver.findElements(element);

        int total = listElement.size();

        if(total > 0){
            return true;
        }

        return false;
    }

    // Wait

    public void waitForPageLoaded(){
        // wait for jQuery to loaded
        ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                try {
                    return ((Long) ((JavascriptExecutor) driver).executeScript("return jQuery.active") == 0);
                } catch (Exception e) {
                    return true;
                }
            }
        };

        // wait for Javascript to loaded
        ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor) driver).executeScript("return document.readyState")
                        .toString().equals("complete");
            }
        };

        try {
            wait = new WebDriverWait(driver, timeoutWaitForPageLoaded);
            wait.until(jQueryLoad);
            wait.until(jsLoad);
        } catch (Throwable error) {
            Assert.fail("Quá thời gian load trang.");
        }

    }

}


[Selenium Java] Bài 23: Viết hàm xử lý chung để dùng lại | Anh Tester


Gọi dùng lại

Ví dụ: SignInPage.class

package anhtester.pages;

import anhtester.common.helpers.ValidateUIHelpers;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

public class SignInPage {

    WebDriver driver;
    private ValidateUIHelpers validateUIHelpers;

    public SignInPage(WebDriver driver) {
        this.driver = driver;
        validateUIHelpers = new ValidateUIHelpers(this.driver);
    }

    //  Element at Sign In page
    private By emailInput = By.id("email");
    private By passwordInput = By.id("password");
    private By signinBtn = By.xpath("//button[@type='submit']");

    public DashboardPage signIn(String emailValue, String passwordValue) {
        validateUIHelpers.waitForPageLoaded();
        Assert.assertTrue(validateUIHelpers.verifyElementText(signinBtn, "Sign in"), "Không phải trang Sign In");
        validateUIHelpers.setText(emailInput, emailValue);
        validateUIHelpers.setText(passwordInput, passwordValue);
        validateUIHelpers.clickElement(signinBtn);

        return new DashboardPage(this.driver);
    }
}


Video phần 2:

  • Anh Tester

    Đường dẫu khó chân vẫn cần bước đi
    Đời dẫu khổ tâm vẫn cần nghĩ thấu

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: new

Filename: post/post_detail.php

Line Number: 384

Backtrace:

File: /home/anhtest2/public_html/application/views/frontend/post/post_detail.php
Line: 384
Function: _error_handler

File: /home/anhtest2/public_html/application/views/frontend/layout/layout_view.php
Line: 370
Function: view

File: /home/anhtest2/public_html/application/core/MY_Controller.php
Line: 34
Function: view

File: /home/anhtest2/public_html/application/controllers/frontend/Post.php
Line: 59
Function: render

File: /home/anhtest2/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Trying to get property 'slug' of non-object

Filename: post/post_detail.php

Line Number: 384

Backtrace:

File: /home/anhtest2/public_html/application/views/frontend/post/post_detail.php
Line: 384
Function: _error_handler

File: /home/anhtest2/public_html/application/views/frontend/layout/layout_view.php
Line: 370
Function: view

File: /home/anhtest2/public_html/application/core/MY_Controller.php
Line: 34
Function: view

File: /home/anhtest2/public_html/application/controllers/frontend/Post.php
Line: 59
Function: render

File: /home/anhtest2/public_html/index.php
Line: 315
Function: require_once

https://anhtester.com//blog/selenium-java/" data-width="100%" data-numposts="4">