- package com.hpp;
- public class sum {
- private int no1;private int no2;
- private int mysum;
- public int add(int no1,int no2){
- mysum=no1+no2;return mysum;
- }
- }
复制代码
4.6再新建testng class
[attach]107320[/attach]
[attach]107321[/attach]
4.7点击finish,代码如下
CODE:
- package com.test;
- import org.testng.annotations.Test;
- import static org.testng.Assert.assertEquals;
- import com.hpp.sum;
- public class NewTest {
- private sum newSum=new sum();
- @Test
- public void f() {
- int mysum=newSum.add(1, 2);
- assertEquals(3,mysum,"Right");
- }
- }
复制代码
testing,xml会自动配置好的,这里不用管项目的文件结构如下:
[attach]107322[/attach]
4.8在testing.xml右键点击RunAs->Testng Suite,即可看到结果
[attach]107323[/attach]
[attach]107324[/attach]
如果想要换个测试用例,可以修改文件testing.xml中class标签name的值
[attach]107325[/attach]
4.9也可以不用在 Testng Suite 模式下运行
新建class,代码如下
run as->Java Application 即可。
[attach]107326[/attach]
CODE:- package com.test;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class Test_google {
- public static void main(String[] args) {
- String url = "http://www.google.com.hk";
- //String url = "http://mobile.service.com/user/CheckLogin.aspx?UserName=aaa@aa.aa&Password=123456&key=889";
- //System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
- //WebDriver driver = new FirefoxDriver();
- //打开ie WebDriver ie_driver = new InternetExplorerDriver();
- //打开chrome
- WebDriver driver = new ChromeDriver();
- driver.get(url);
- WebElement element = driver.findElement(By.name("q"));
- element.sendKeys("hello Selenium!");
- element.submit();
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("页面Title:" + driver.getTitle() + "\n页面URL:"
- + driver.getCurrentUrl());
- //System.out.println("返回当前的浏览器的窗口句柄:"+driver.getWindowHandle());
- //String s=driver.getPageSource();s=s.substring(s.indexOf("{"), s.indexOf("}"));
- //System.out.println("当前页面的源码:"+s);
- driver.quit();
- }
- }
复制代码4.10 如果要在Myeclipse安装插件Testng
直接把eclipse里的org.testng文件夹拷贝到dropins目录
重启Myeclipse会提示如下,确定就OK了
[attach]107327[/attach]
5.Eclipse中导入jar文件的源码
在eclipse中导入jar文件对应的源码,主要是为了方便查看一些接口的源码,可以直接按着Ctrl+鼠标左键跳到对应的源码文件。
若无法查看类,函数等信息
[attach]107328[/attach]
将下载selenium-java-2.21.0包中的selenium-java-2.21.0-srcs.jar导入
[attach]107329[/attach]