选择比努力更重要,责任比能力更重要,情商比智商更重要
基于testng框架的web自动化测试
package baidutest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class BaiduTest {
    WebDriver driver;//new FirefoxDriver();
    
    @BeforeClass
    public void setup() 
    {
        //讲谷歌驱动拷贝到项目下
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.baidu.com/");
        //driver.manage().window().maximize();
    }
    @Test
    public void test1()
    {
        //输入selenium查询
        driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("百度");
        //点击百度一下
        driver.findElement(By.xpath(".//*[@id='su']")).click();
        System.out.println("测试用例1");    
    }
    @Test
    public void test2()
    {
        System.out.println("测试用例2");
    }
    @AfterClass
    public void closed()
    {
        //关闭浏览器
        driver.quit();
        System.out.println("关闭浏览器成功");
    }
}
xpath定位元素的几种方法
1、contains关键字
driver.findElement(By.xpath("//a[contains(@href, 'logout')]"));
这句话的意思是寻找页面中href属性值包含有logout这个单词的所有a元素。
2、starts-with关键字
driver.findElement(By.xpath("//a[starts-with(@rel, 'nofo')]"));
这句的意思是寻找rel属性以nofo开头的a元素。其中@后面的rel可以替换成元素的任意其他属性。
3、text使用
driver.findElement(By.xpath("//*[text()='退出']"));
直接查找页面当中所有的退出二字,根本就不用知道它是个a元素了。适用纯文字查找。
4、同级目录组合查找
driver.findElement(By.xpath("//*[@id='verificationCode']/../..//*[@id='userName']"));
同级目录组合,../标识上级目录,//是根元素。

posted on 2017-05-27 15:24  测试小强  阅读(1055)  评论(0)    收藏  举报