天天看點

Selenium web driver 配合使用TestNG

首先為eclipse添加testng插件

  1.步驟如下:help->install new software...

Selenium web driver 配合使用TestNG

  2. 添加testng連結,該連結可以在這裡找到

for the eclipse plug-in, we suggest using the update site:

select help / software updates / find and install.

search for new features to install.

new remote site.

for eclipse 3.4 and above, enter http://beust.com/eclipse.

for eclipse 3.3 and below, enter http://beust.com/eclipse1.

make sure the check box next to url is checked and click next.

eclipse will then guide you through the process.

Selenium web driver 配合使用TestNG

<a href="http://www.51testing.com/batch.download.php?aid=45234" target="_blank"></a>

Selenium web driver 配合使用TestNG

  testng和junit相比為什麼要使用testng

  1. testng 和junit功能基本相同testng支援suite,junit執行一大堆case,如果個别fail,隻能所有case重跑

  而testng可以單獨跑

  2.testng能生成漂亮的測試報告,比較直覺

  添加一個testng case,複制一份,儲存為

  openlinktest1.java

package baidu;

import java.io.file;

import java.io.ioexception;

import java.util.regex.matcher;

import java.util.regex.pattern;

import junit.framework.assert;

import org.apache.commons.io.fileutils;

import org.openqa.selenium.by;

import org.openqa.selenium.outputtype;

import org.openqa.selenium.takesscreenshot;

import org.openqa.selenium.webdriver;

import org.openqa.selenium.webelement;

import org.openqa.selenium.chrome.chromedriver;

import org.testng.annotations.aftermethod;

import org.testng.annotations.beforemethod;

import org.testng.annotations.test;

public class openlinktest {

public static void snapshot(takesscreenshot drivername, string filename)

{

// this method will take screen shot ,require two parameters ,one is driver name, another is file name

file scrfile = drivername.getscreenshotas(outputtype.file);

// now you can do whatever you need to do with it, for example copy somewhere

try {

system.out.println("save snapshot path is:e:/"+filename);

fileutils.copyfile(scrfile, new file("e:\\"+filename));

} catch (ioexception e) {

// todo auto-generated catch block

system.out.println("can't save screenshot");

e.printstacktrace();

}

finally

system.out.println("screen shot finished");

@beforemethod

public void tearup()

//    webdriver driver = new chromedriver();

@test

public static void runselenium() throws interruptedexception

string url="http://www.baidu.com";

pattern p = pattern.compile("http");

matcher m = p.matcher(url);

if(m.find())

system.out.println(url);

system.setproperty("webdriver.chrome.driver", "e:\\chromedriver.exe");

webdriver driver = new chromedriver();

driver.get(url);

//max size the browser

driver.manage().window().maximize();

/*

navigation navigation = driver.navigate();

navigation.to(url);*/

thread.sleep(2000);

snapshot((takesscreenshot)driver,"open_baidu.png");

//webelement reg=driver.findelement(by.name("tj_reg"));

//reg.click();

//    webelement keyword = driver.findelement(by.id("kw1"));

//find the element

webelement keyword = driver.findelement(by.xpath("//input[@id='kw1']"));

keyword.clear();

//send key words

keyword.sendkeys("selenium");

thread.sleep(3000);

snapshot((takesscreenshot)driver,"input_keyword.png");

webelement submit = driver.findelement(by.id("su1"));

system.out.println(submit.getlocation());

submit.click();

//system.out.println(driver.getwindowhandle());

thread.sleep(5000);

// system.out.println(driver.getpagesource());

string pagesource=driver.getpagesource();

//  system.out.println(pagesource);

//webelement link =driver.findelement(by.xpath(selenium_link));

webelement link =driver.findelement(by.xpath("//*[@id=\"1\"]/h3/a"));     //*[@id="1"]/h3/a

link.click();

driver.switchto().window(driver.getwindowhandles().toarray(new string[0])[1]);

//get page title

system.out.println(driver.gettitle());

//     navigation.back();

snapshot((takesscreenshot)driver,"open_bake.png");

system.out.println(driver.gettitle()+"\n"+driver.getcurrenturl());

assert.assertequals(driver.gettitle(),"selenium - web browser automation");

driver.quit();

@aftermethod

public void teardown()

//driver.quit();

system.out.println("------------end----------------------");

 添加testng suite

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;suite name="suite" parallel="false"&gt;

&lt;test name="test"&gt;

&lt;classes&gt;

&lt;class name="baidu.openlinktest"/&gt;

&lt;class name="baidu.openlinktest1"/&gt;

&lt;/classes&gt;

&lt;/test&gt; &lt;!-- test --&gt;

&lt;/suite&gt; &lt;!-- suite --&gt;

  執行case:

Selenium web driver 配合使用TestNG

  結果如下

[testng] running:

c:\users\young\workspace\selenium\src\baidu\testng.xml

http://www.baidu.com

starting chromedriver (v2.9.248315) on port 28218

save snapshot path is:e:/open_baidu.png

screen shot finished

save snapshot path is:e:/input_keyword.png

(858, 179)

selenium - web browser automation

save snapshot path is:e:/open_bake.png

http://docs.seleniumhq.org/

------------end----------------------

starting chromedriver (v2.9.248315) on port 5568

selenium_百度百科

http://baike.baidu.com/subview/478050/6464537.htm?fr=aladdin

===============================================

suite

total tests run: 2, failures: 1, skips: 0

最新内容請見作者的github頁:http://qaseven.github.io/

繼續閱讀