I. Giới thiệu về AUTO IT và cách cài đặt
1. AUTO IT là gì ?
- Do selenium không thể làm việc được với Window Dialog mà chỉ làm việc được với web app do đó Auto IT sẽ giúp chúng ta gửi file patch vào file name và click vào open button.
- Nguyên lý hoạt động sẽ như sau: Chúng ta sẽ tạo ra một file test script upload file có đuôi chấm au3 được viết bằng ngôn ngữ Visual Basic, sau đó convert file này sang file có đuôi exe. Tiếp theo sẽ viết code truyền vào file exe này để có thể upload file trong Window Dialog.
2. Cách cài đặt AUTO IT
- Để download AUTO IT chúng ta vào link: https://www.autoitscript.com/site/autoit/downloads/
- Sau khi cài đặt xong thì sẽ xuất hiện thư mục Auto IT tại đường dẫn: C:\Program Files (x86)\AutoIt3
- Trong này có một số Folder:
Folder Aut2Exe: Là tool để convert thì file au3 sang exe
Folder Example: Là Folder chứa các ví dụ, chúng ta có thể xem nó ở trong file AutoIt.chm là help của AutoIT, nó sẽ có help cho từng command của nó.
Folder Include: Để include các thư viện cho Auto IT
Au3Info.exe: Các tool này giúp inspect các element trong window dialog
3. Phân tích file ví dụ HandleUploadForm.au3
File này được viết bằng ngôn ngữ Visual Basic
Dim $WindowTitle = $CmdLine[1] //Khai báo ra một biến WindowTitle, được truyền giá trị CmdLine
Dim $FilePath = $CmdLine[2] // Khai báo ra một biến FilePath, được truyền giá trị CmdLine
Dim $TimeOut = $CmdLine[3] // Khai báo ra một biến TimeOut, được truyền giá trị CmdLine, biến này có tác dụng nếu quá lâu mà nó không kết thúc được thì nó hết khoảng thời gian timeout sẽ thoát ra và báo lỗi
Dim $error = 0
$error = WinWaitActive($WindowTitle, "", $TimeOut) // HàmWinWaitActive() có tác dụng đợi đến khi nào cái Window có Title này nó active lên.
Sleep(1000)
If $error = 0 Then Exit (1)
ControlSend($WindowTitle, "", "[CLASS:Edit; INSTANCE:1]", $FilePath) // Hàm ControlSend() để send key vào window mà có tile này.
Trong AutoIt Help thì nó nằm trong mục Window Management - Controls - ControlSend
Cú pháp: ControlSend ( "title", "text", controlID, "string" [, flag =
0] )
Để tìm controlID chúng ra sẽ dùng tool Au3Info.exe, muốn tìm controlID của chỗ nào thì chúng ta nhấn vào Finder Tool và kéo vào chỗ chúng ta muốn tìm StringID
ControlClick($WindowTitle, "", "[CLASS:Button; INSTANCE:1]") // Click chuột vào đúng cái control
Cú pháp: ControlClick ( "title", "text", controlID [, button = "left"
[, clicks = 1 [, x [, y]]]] )
$error = WinWaitClose($WindowTitle, "", $TimeOut)// Đợi cho đến khi nào cửa sổ nó đóng lại, nếu không đóng lại thì có khoảng thời gian timeout và nó sẽ báo lỗi
If $error = 0 Then Exit (1) Else Exit (0)
Hình 2: Giao diện tool Au3Info.exe
Hình 3: Cách kéo Finder Tool để lấy ControlID
4. Dịch file Au3 sang file exe
Mở folder Aut2Exe vào chạy file Aut2exe.exe, sau đó mở đến local chứa file Au3 muốn chuyển đổi và nhấn convert. Sau khi convert xong thì chúng ta sẽ có file exe và nhiệm vụ của chúng ta là phải gọi đến được file đó trong phần code selenium.
Hình 4: Tool Aut2Exe
II. Ví dụ về Auto IT
package test;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
public class ReportIssueTest {
WebDriver driver;
@Test
public void reportIssueTest() throws IOException, InterruptedException {
// Login
driver.findElement(By.id("username")).sendKeys("XXXXXX");
driver.findElement(By.cssSelector("input[value='Login']")).click();
driver.findElement(By.id("password")).sendKeys("XXXXXX");
driver.findElement(By.cssSelector("input[value='Login']")).click();
// Report issue
driver.findElement(By.cssSelector("#sidebar > ul >li:nth-child(3) > a")).click();
Select sel_Project = new Select(driver.findElement(By.id("select-project-id")));
sel_Project.selectByVisibleText("BMS");
driver.findElement(By.cssSelector("input[value='Select Project']")).click();
new Select(driver.findElement(By.id("category_id"))).selectByVisibleText("[All Projects] General");
new Select(driver.findElement(By.id("reproducibility"))).selectByVisibleText("always");
new Select(driver.findElement(By.id("severity"))).selectByVisibleText("major");
new Select(driver.findElement(By.id("priority"))).selectByVisibleText("high");
driver.findElement(By.id("summary")).sendKeys("Selenium Test");
driver.findElement(By.id("description")).sendKeys("Day la Auto Test\nTao Boi HienDV94");
driver.findElement(By.id("steps_to_reproduce")).sendKeys("Step1\nStep2\nStep3");
driver.findElement(By.id("additional_info"))
.sendKeys("Day la thong tin bo sung 1\nDay la thong tin bo sung 2\n");
//Upload File
driver.findElement(By.className("dropzone")).click();
Process p = new ProcessBuilder("C:\\Users\\HienDV94\\eclipse-workspace\\AutoIT Test\\lib\\HandleUploadForm.exe", "Open", "D:\\Work\\Automation Test\\Selenium\\Tool\\Selenium\\Library\\Auto IT\\textAutoIT.txt", "30").start();
// Chúng ta sẽ tạo ra một đối tượng p có kiểu là Process, trong hàm ProcessBuilder sẽ truyển vào ProcessBuilder("Đường dẫn tới File Script sau khi đã biên dịch sang exe", "Window Title", "Đường dẫn file cần upload", "Thời gian Timeout")
p.waitFor();// Gọi để cho sau khi chạy process xong thì nó phải đợi cho process này kết thúc(Kiểu upload file, đóng cửa sổ xong) chứ không chạy luôn tiếp
driver.findElement(By.cssSelector(".table-responsive >table>tbody>tr:nth-child(13)>td>label:nth-child(2)>span"))
.click();
driver.findElement(By.cssSelector(".table-responsive >table>tbody>tr:nth-child(14)>td>label>span")).click();
driver.findElement(By.cssSelector("input[value='Submit Issue']")).click();
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver",
"D:\\Work\\Automation Test\\Selenium\\Tool\\Selenium\\Library\\Selenium-java-3.141.59 and Driver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://testerhanoi.com/login_page.php");
}
@AfterMethod
public void afterMethod() {
// driver.quit();
}
}
0 Bình luận:
Đăng nhận xét