Ở bài viết này chúng ta sẽ tìm hiểu cách tạo script với WebDriver. Mình sử dụng http://www.calculator.net/ để làm ví dụ cụ thể, thực hiện tính phần trăm một cách tự động. Bạn hãy chắc chắn rằng mình đã download WebDriver JAR’s. Nếu chưa bạn có thể tham khảo bài viết “Môi trường cài đặt” để biết thêm chi tiết.
Bước 1: Khởi động “Eclipse” từ thư mục Eclipse bạn đã giải nén.
Bước 2: Tạo một project mới từ menu: File -> New-> Java Project.
Bước 3: Nhập tên project và click “Next”.
Bước 4: Chọn tab “Libraries” và chọn “Add External JARs” tất cả những file JAR mà đã download ở phần hướng dẫn cài đặt trước –> Finish.
Nhớ add cả những file JAR trong thư mục “libs” nữa nhé.
Bước 5: Click chuột phải vào package và tạo một Class mới như hình sau.
Bước 6: Đặt tên cho class và chọn tạo phương thức main -> Finish.
Bước 7: Sau khi click finish, class vừa tạo hiển thị như sau.
Bước 8: ví dụ về script sau khá dễ hiểu, đã được chú thích rõ ràng, bạn hãy gõ lại và chạy thử nhé. Để hiểu hơn các thuộc tính đối tượng, bạn hãy xem bài viết “Locators”.
Test trên Firefox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class webdriverdemo { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); // Puts an Implicit wait, Will wait for 10 seconds before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Launch website driver.navigate().to("http://www.calculator.net/"); // Maximize the browser driver.manage().window().maximize(); // Click on Math Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Percent Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 50 in the second number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Click Calculate Button driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr[2]/td/input[2]")).click(); // Get the Result Text based on its xpath String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/font/b")).getText(); // Print a Log In message to the screen System.out.println(" The Result is " + result); // Close the Browser. driver.close(); } } |
Test trên Chrome: ở bài viết trước mình đã hướng dẫn download chromedriver, bạn hãy xác định vị trí lưu nó và thay đổi đoạn code dưới đây nhé.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; public class webdriverdemo { public static void main(String[] args) { // Change your URL of chromedriver System.setProperty("webdriver.chrome.driver", "E:/thuong-data/Selenium/libs/chromedriver.exe"); WebDriver driver = new ChromeDriver(); // Puts an Implicit wait, Will wait for 10 seconds before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Launch website driver.navigate().to("http://www.calculator.net/"); // Maximize the browser driver.manage().window().maximize(); // Click on Math Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Percent Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 50 in the second number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Click Calculate Button driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr[2]/td/input[2]")).click(); // Get the Result Text based on its xpath String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/font/b")).getText(); // Print a Log In message to the screen System.out.println(" The Result is " + result); // Close the Browser. driver.close(); } } |
Internet Explorer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.openqa.selenium.ie.InternetExplorerDriver; public class browsersdemo { public static void main(String[] args) { System.setProperty("webdriver.ie.driver", "E:/thuong-data/Selenium/libs/IEDriverServer.exe"); WebDriver driver = new InternetExplorerDriver(); // Open tzongreen site driver.get("http://tzongreen.com"); // Maximize the browser driver.manage().window().maximize(); } } |
Bước 9: Kết quả của script trên sẽ được in trong Console.
Một số command được sử dụng nhiều nhất
Bảng sau đây liệt kê một số lệnh được sử dụng thường xuyên nhất trong WebDriver.
Command | Description |
driver.get(“URL”) | Điều hướng tới một ứng dụng. |
element.sendKeys(“inputtext”) | Nhập một số văn bản vào một ô nhập liệu. |
element.clear() | Xóa tất cả nội dụng của một ô nhập liệu. |
select.deselectAll() | Bỏ chọn tất cả các tùy chọn của SELECT đầu tiên trên trang. |
select.selectByVisibleText(“some text”) | Chọn OPTION với đầu vào được xác định bởi người dùng. |
driver.switchTo().window(“windowName”) | Di chuyển trọng tâm từ tới một cửa sổ khác. |
driver.switchTo().frame(“frameName”) | Swing from frame to frame. |
driver.switchTo().alert() | Helps in handling alerts. |
driver.navigate().to(“URL”) | Điều hướng đến một URL. |
driver.navigate().forward() | Điều hướng tới trang trước (forward). |
driver.navigate().back() | Điều hướng tới trang sau (back). |
driver.close() | Đóng trình duyệt hiện tại có liên kết với trình điều khiển. |
driver.quit() | Thoát trình điều khiển và đóng tất cả các sử sổ liên kết tới trình điều khiển. |
driver.navigate().refresh() | Làm mới trang hiện tại. |
Nguồn: tutorialspoint