Tag Archives: selenium
September 27, 2011 Framework Selenium RC, TestNG, ReportNG
1. Install: JAVA, Eclipse, Firebug (addon of Firefox)
2. Download example here and document here
3. Download library for project (nếu đẫ down ví dụ về thì có sẵn rồi khỏi down nữa)
http://www.4shared.com/file/Ljh_5FHY/velocity-dep-14.html?
http://www.4shared.com/file/pRtjbdKK/testng-611.html?
http://www.4shared.com/file/axrUCsDB/selenium-server.html?
http://www.4shared.com/file/VEg0RVoV/selenium-java-client-driver.html?
http://www.4shared.com/file/QmukhteM/reportng-113.html?
http://www.4shared.com/file/LNXUCADB/log4j-1216.html?
4. Download selenium server standalone http://www.4shared.com/file/wODtBHgl/selenium-server-standalone-210.html?
5. Tạo project ví dụ
6. Tạo thư mục lib (nhấp chuột phải vào tên project và chọn New > Folder). sau đó copy các gói jar đã donwload về và paste vào thư mục này. Sau đó quét khối các gói jar đó và nhập chuột phải chọn Build path > Add to build path
Tạo thư mục testng_output
7. Nhấp chuột phải vào thư mục src và chọn New > Package. Tạo 3 package: com , com.module, com.utility
package com dùng để chứa lớp main, sẽ start chạy automation
package module dùng để chứa script automation của những module hoặc page khác
package utility dùng để chửa 1 số script sử dụng chung, …
8. Nhấp chuột phải vào package com, chọn New > Class , nhập name là Automation_Starter. Xem script trong example
9. Nhấp chuột phải và project và chọn New > File , nhập tên là build.xml . (đừng thay đổi gì ở file này hết và đã tối ưu nhất)
10. Nhấp chuột phải và project và chọn New > File , nhập tên là testng.xml, code như vầy:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <listeners> <listener class-name="com.utility.Screenshot"></listener> </listeners> <!-- config selenium --> <parameter name="host" value="localhost" /> <parameter name="port" value="4444" /> <parameter name="browser" value="*chrome" /> <parameter name="admin_url" value="http://translate.google.com/" /> <test name="Automation_Example" preserve-order="true"> <classes> <class name="com.Automation_Starter" /> </classes> </test> <test name="GTranslate" preserve-order="true"> <classes> <class name="com.module.GTranslate" > <methods> <include name="open" /> </methods> </class> </classes> </test> </suite>
Listener dùng để chụp hình nếu testcase đó chạy bị lỗi
Phần config selenium là cấu hình đường dẫn url, trình duyệt…
Selenium sẽ chạy từ trên xuống dưới theo cấu hình của file testng.xml
Mỗi thẻ <test … là tương ứng với 1 class, bạn có thể tạo nhìu method trong class đó, và dùng thẻ include để selenium chạy cái method này.
11. Nhấp chuột phải vào com.utility và chọn New > Class, đặt tên là Screenshot.java
12. Nhấp chuột phải và project và chọn New > File , nhập tên là log4j.properties
13. Tạo 1 testcase ví dụ GTranslate.java trong com.module
Cấu trúc sau khi tạo sẽ như vậy, code thì mở ví dụ ra xem nha
13. Chạy file selenium server standalone
14. Nhập chuột phải vào file build.xml và chọn Run As > Ant
15. Sau khi chạy xong xem kết quả AutomationFramework/testng_output/html/index.html
16. Tài liệu về selenium xem tại đây
1 số điều cần lưu ý:
- Dùng firebug để dò Id hoặc XPath của element trên web
- Cần nắm sơ qua về cơ bản selenium, như thao tác click, nhập liệu, di chuyển trang…
- Lưu ý khi dùng waitForPageLoad
- Xem ví dụ nếu chưa hiều thì xem file word hướng dẫn của mình cho rõ thêm
- Bài tập về nhà: test cái page login của yahoo. Làm xong ví dụ này thì có thể an tâm đi pv hehe
- Đây là framework automation do mình viết, rất dễ sử dụng, đúc kết sau bao nhiu năm cống hiến, cấm copy sao chép dưới mọi hình thức hohoho 🙂
- http://www.openwritings.net/public/selenium-2/first-selenium-2-application-java , tham khảo thêm
Tags: ReportNG, selenium, Selenium RC, TestNG
- 8 comments
- Posted under AutoIT, RobotFramework
July 22, 2011 Using Selenium RC with multi testcase
1. Class MyAuto is class to run Selenium. It will class TCManager to manage flow of testcase
package com.testscripts; import com.thoughtworks.selenium.SeleneseTestCase; public class MyAuto extends SeleneseTestCase { private TCManager tcm; public void setUp() throws Exception { setUp("http://google.com/", "*chrome"); } public void testRun() throws Exception { tcm = new TCManager(this.selenium); tcm.testcase1(); } }
2. Here is TCManager, it also read value in excel file
package com.testscripts; import com.thoughtworks.selenium.SeleneseTestCase; import com.thoughtworks.selenium.Selenium; public class TCManager { private Selenium selenium; private Excel excel; public TCManager(Selenium _selenium) { selenium = _selenium; excel = new Excel(); } public void testcase1() { selenium.open("/"); selenium.windowMaximize(); selenium.type("q", "selenium IDE"); selenium.click("btnG"); excel.openConnection(); excel.setSheet("testsheet"); int row = 0; int col = 1; System.out.println( excel.getValueFromExcel(row,col) ); pause(5000); excel.closeConnection(); SeleneseTestCase.assertEquals(false, true); } public void pause(long milisecond) { try { Thread.sleep(milisecond); } catch (Exception e) { System.out.println("TCManager\\pause: " + e.toString()); } } }
3. Here is class Excel file
package com.testscripts; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Excel { private Connection con; private ResultSet rs; public String dbURL = "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};" + "DBQ= "+ "resource\\FBFriends.xls;" + "DriverID=22;READONLY=false"; public void openConnection() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection(dbURL); } catch (Exception e) { System.out.println("Excel\\OpenConnection: " + e.toString()); } } public void closeConnection() { if (con != null) { try { rs.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void setSheet(String sheetName) { if(con == null) { System.out.println("Not able to connect to MS EXCEL"); return; } try { Statement stmnt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); String query = "select * from [" + sheetName + "$]" ; rs = stmnt.executeQuery( query ); } catch (Exception e) { System.out.println("Excel\\setSheet: " + e.toString()); } } public ResultSet getResultSet() { return rs; } public String getValueFromExcel(int rowIndex,int colIndex) { String value = ""; try { rs.absolute(rowIndex); value = rs.getString(colIndex); } catch (SQLException e) { System.out.println("Excel\\getValueFromExcel: " + e.toString()); } return value; } }
Tags: selenium, Selenium RC
- Leave a comment
- Posted under AutoIT, RobotFramework