Skip to content

Crazy blog

No money, no honey, no funny…

Category Archives: RobotFramework

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: , , ,

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: ,

This topic is useful with newbie of RobotFramework, I just apply it for my project, and it looks perfect.

Run Keyword: execute a user keyword, and stop all test if FAIL.

Run Keyword If: execute a keyword with IF condition, and stop all test if FAIL, this is very useful.

Run Keyword And Continue On Failure: execute a keyword, and continue always, don’t care if FAIL, but will stop if ERROR. Example, click on element, but element not exist. Error will display and the test will stop. If the keyword run FAIL, if will inform FAIL on command line, then continue run another testcase.

Run Keyword And Ignore Error: execute a keyword, and return result include information about PASS or FAIL, and what make it FAIL, then continue testcase. This is very useful when you want to know the keyword run success or not, then do something or generate your custom report.

End.

Tags:

Today, I have the problem when assign value for a variable in using robotframework. It takes me 6 hours to investigate. And I see the problem that I just use variable on the normal way:Set Variable. So that the problems happen that I assign value but it always return empty.

Here is the example to solve that problem. It is just set the ${MyVariable} with value 100 from Resource Temp.html. Then write it to file.
In testcase, using Set Global Variable ${MyVariable} 10 (default value)

In Resource Temp.html, just call Set Global Variable ${MyVariable} 100 (new value)

Then just run the script and view the file ${file}. The result will be 100. 🙂

Tags: ,

1. Download all links bellow to set up for win 32 bit
http://www.general-search.com/download/jre6u22-windows-i586
http://www.python.org/ftp/python/2.7/python-2.7.msi
http://sourceforge.net/projects/wxpython/files/wxPython/2.9.1.1/wxPython2.9-win32-2.9.1.1-py27.exe/download
http://code.google.com/p/robotframework/downloads/detail?name=robotframework-2.5.4.win32.exe
http://code.google.com/p/robotframework-ride/downloads/detail?name=robotide-0.29.win32.exe
http://code.google.com/p/robotframework-seleniumlibrary/downloads/detail?name=robotframework-seleniumlibrary-2.4.win32.exe

2. Install all with default path
3. Environments Variables.
(Example for win7: Right click My computer, click Advanced system setting, click tab Advanced, click button Environment Variable, at system variable, search variable Path, click Edit button, add C:\Python27\Scripts\, then Ok, Ok, Ok

4. Start RIDE
Go to C:\Python27\Scripts, search file ride.pyo, double click to start RIDE

Tags: