[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords

✅ Các Keywords có sẵn trong akaAT Studio
✅ Sử dụng lại Keywords có sẵn vào Custom Keywords

✅ Các Keywords có sẵn trong akaAT Studio


✳️ General Class


🔆 Init WebDriver and WebElement with WebDriverUtilities

Các bạn có thể dùng RemoteWebDriver để nhận hoặc WebDriver đều được.

Chúng ta hoàn toàn có thể chuyển từ UIObject có sẵn trong tools về dạng WebElement tuỳ ý với dạng simple WebElement và List WebElement

public void getWebElementDemo(UIObject target) {
    //Get WebDriver
    RemoteWebDriver webDriver = WebDriverUtilities.getDriver(); // throw Exception and Failed Step if can NOT get WebDriver
    RemoteWebDriver webDriver = WebDriverUtilities.getDriverQuietly(); // return null if can NOT get WebDriver

    //get the 1st WebElement form UIObject. If UIObject NOT found, throw exception and  fail step
    WebElement element = target.convertToWebElement(webDriver);
    //get the 1st WebElement from UIObject, if UIObject NOT found, return element = null
    WebElement element2 = target.convertToWebElementQuietly(webDriver);
    /*get the 1st WebElement from UIObject, execute continious in timeout = 5s and return as soon as possible
    If element NOT found, throw exception and fail step*/
    WebElement element3 = target.convertToWebElementWithTimeout(webDriver, 5);
    /*get the 1st WebElement from UIObject, execute continious in timeout = 5s and return as soon as possible
    If element NOT found, return element = null*/
    WebElement element4 = target.convertToWebElementWithTimeoutQuietly(webDriver, 5);
    /*get list WebElement from UIObject
    If UIObject not found, return null*/
    List<WebElement> element5 = target.convertToWebElementList(webDriver);
    /*get list WebElement from UIObject, execute continious in timeout = 5s and return as soon as possible
    If UIObject NOT found, return “ ” */
    List<WebElement> element6 = target.convertToWebElementListWithTimeout(webDriver, 5);

}


Lưu ý chung là nhớ import mấy thư viện vào cho đầy đủ. Hàm nào có tham số gì thì nhìn cho kĩ rồi điền vào cho phù hợp !!

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester


🔆 Assert with KeywordAssertion

Keyword này hỗ trợ để xác nhận vấn đề đúng sai trong code. Cực kỳ quan trọng chổ dùng các Assert này. Khi đó chúng ta mới biết được là pass hay fail khi chạy auto test.

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester

🔆 Handle WebElement with ElementActionKeyword

Công cụ akaAT Studio hỗ trợ nhiều phương thức trong lớp ElementActionKeyword để tương tác với một phần tử giao diện người dùng cho 3 nền tảng chính: Web, Mobile, Desktop.

Chổ này thì các bạn sẽ thấy chúng ta dùng trên Web sẽ thấy keyword ElementActionKeyword hỗ trợ sẵn các hàm như trong Selenium để thao tác với WebElement.

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester
[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester

🔆 Wait Element with WaitForKeyword

akaAT Studio hỗ trợ keyword WaitForKeyword trên 3 nền tảng chính. Chúng ta chọn nền tảng dành cho Web keyword.

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester


akaAT Studio
xây dựng sẵn một số hàm chờ đợi dành cho Element như waitForElementClickable, waitForElementVisible với 2 hàm này là đủ để chúng ta thao tác chờ đợi chính rồi.

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester
[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester



✳️ Web Testing


🔆 AlertKeyword

akaAT Studio hỗ trợ lớp AlertKeyword để xử lý cảnh báo Alert trên website.

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester

alertKeyword.acceptAlert();  //To click on the "OK" button of the alert
alertKeyword.dismissAlert();  // To click on the "Cancel" button of the alert.
alertKeyword.getAlertText();  // To get the alert message.
alertKeyword.setTextOnAlert(text);  // To send some data to alert box.

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester

🔆 BrowserKeyword

akaAT Studio hỗ trợ lớp BrowserKeyword để xử lý các hoạt động chính về trình duyệt như openBrowser, back, refresh,...

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester

🔆 BrowserWindowKeyword

akaAT Studio hỗ trợ lớp BrowserWindowKeyword để xử lý cửa sổ Popup Window, Tab và Frame.

Một số hàm thường dùng như: maximizeCurrentWindow, switchToFrame, openNewTab, closeCurrentWindow,...

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester
[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester

🔆 FormActionKeyword

akaAT Studio hỗ trợ lớp FormActionKeyword để xử lý các hành động về Form nhập liệu và Dropdown list.

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester
[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester

✳️ Random keywords catalog


🔆 RandomDataKeyword

akaAT Studio hỗ trợ lớp RandomDataKeyword để generate data tự động ngẫu nhiên với nhiều kiểu dữ liệu khác nhau như Integer, String, DateTime, Email,...

[akaAT Studio] Bài 18: Sử dụng lại Keywords của akaAT vào Custom Keywords | Anh Tester

Yeah còn rất nhiều, một lúc An khó chia sẻ hết được. Chưa kể là tools sẽ còn update và phát triển thời gian tới dài dài nên các bạn theo dõi nghiên cứu thêm nhé.


✅ Sử dụng lại Keywords có sẵn trong Custom Keywords

An viết một số hàm xử lý cho hệ thống CMS mẫu nhé:

  • loginCMS : dùng để truyền email và password vào sau đó login
  • highLightObject : để highlight object với border màu (màu đỏ ví dụ)
  • clickObject : chờ đợi object hiển thị sẵn sàng xong mới click
  • clickAndHighlight : highlight xong click vào object
  • sendKeysAndHighlight : highlight xong điền text vào textbox

package anhtester.keywords;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.RemoteWebDriver;

import com.fpt.ivs.at.core.keywords.webkeyword.WaitForKeyword;
import com.fpt.ivs.at.core.object.UIObject;
import com.fpt.ivs.at.core.utilities.WebDriverUtilities;

public class SeleniumKeywords {

	public void loginCMS(String email, String password) {
		RemoteWebDriver driver = WebDriverUtilities.getDriver();

		driver.manage().window().maximize();
		driver.get("https://cms.anhtester.com/login");
		driver.findElement(By.xpath("//input[@id='email']")).sendKeys(email);
		driver.findElement(By.xpath("//input[@id='password']")).sendKeys(password);
		driver.findElement(By.xpath("//button[normalize-space()='Login']")).click();
	}

	public void highLightObject(UIObject target) {
		RemoteWebDriver driver = WebDriverUtilities.getDriver();

		String borderDefault = target.convertToWebElement(driver).getCssValue("border");
		System.out.println(borderDefault);

		// Tô màu border ngoài chính element chỉ định - màu Đỏ (có thể đổi màu khác)
		if (driver instanceof JavascriptExecutor) {
			((JavascriptExecutor) driver).executeScript("arguments[0].style.border='2px solid red'",
					target.convertToWebElement(driver));
		}

		// Chờ đợi 1 giây
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		// Trả lại border cũ mặc định
		((JavascriptExecutor) driver).executeScript("arguments[0].style.border='" + borderDefault + "'",
				target.convertToWebElement(driver));
	}

	public void clickObject(UIObject target) {

		// Chờ đợi Element visible
		WaitForKeyword waitForKeyword = new WaitForKeyword();
		waitForKeyword.waitForElementVisible(target, 10);

		RemoteWebDriver driver = WebDriverUtilities.getDriver();
		target.convertToWebElement(driver).click();

	}

	public void clickAndHighlight(UIObject target) {

		// Chờ đợi object visible
		WaitForKeyword waitForKeyword = new WaitForKeyword();
		waitForKeyword.waitForElementVisible(target, 10);

		// Highlight object
		highLightObject(target);

		RemoteWebDriver driver = WebDriverUtilities.getDriver();
		target.convertToWebElement(driver).click();

	}

	public void sendKeysAndHighlight(UIObject target, String value) {

		// Chờ đợi object visible
		WaitForKeyword waitForKeyword = new WaitForKeyword();
		waitForKeyword.waitForElementVisible(target, 10);

		// Highlight object
		highLightObject(target);

		RemoteWebDriver driver = WebDriverUtilities.getDriver();
		target.convertToWebElement(driver).sendKeys(value);

	}

}


✳️ Source code: https://github.com/anhtester/akaATStudioLearningAnhTester2023

  • 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