使用Selenium PageFactory页面工厂的好处是:

当页面元素的位置发生变化时,

我们只需要去修改id或者xpath,

而不用去修改测试用例。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; public class TestNG {
private WebDriver driver; @FindBy(xpath = ".//*[@id='kw']")
private WebElement inputBox;
//输入框 @FindBy(xpath = ".//*[@id='su']")
private WebElement searchButton;
//搜索按钮 @BeforeMethod
public void beforeMethod() throws InterruptedException {
System.setProperty("webdriver.firefox.marionette",
"src/main/resourcec/geckodriver.exe");
String baiduHomePage;
baiduHomePage = "https://www.baidu.com/"; driver = new FirefoxDriver();
PageFactory.initElements(driver, this);
//构造函数,初始化PageFactory对象
driver.manage().window().maximize();
driver.get(baiduHomePage);
Thread.sleep(2000);
Assert.assertEquals(driver.getTitle(), "百度一下,你就知道");
} @Test
public void testNG() throws InterruptedException {
inputBox.clear();
inputBox.sendKeys("Selenium"); searchButton.click();
Thread.sleep(3000); Reporter.log("搜索Selenium的测试用例");
Assert.assertEquals(driver.getTitle(), "Selenium_百度搜索");
} @AfterMethod
public void afterMethod(){
driver.close();
driver.quit();
} }

Selenium PageFactory页面工厂的更多相关文章

  1. Selenium页面工厂+数据驱动测试框架

    工程的目录结构: pom.xml文件: <?xml version="1.0" encoding="UTF-8"?><project xmln ...

  2. Selenium高亮页面对象

    使用QTP习惯了,在QTP中可以通过访问对象的highlight方法直接高亮对象,确实很方便,那么如何让Selenium高亮页面的测试对象了,可以通过javascript修改页面对象的属性进而高亮对象 ...

  3. Selenium处理页面---弹窗、表格、鼠标悬停、frame、下拉框、上传文件

    一.Selenium测试-常用页面处理 1.概述 UI自动化测试(GUI界面层):UI层是用户使用产品的入口,所有功能通过这一层提供给用户,测试工作大多集中在这一层,常见的测试工具有UFT.Robot ...

  4. 精简版 Selenium PageFactory, Annotation 实例

    精简版 Selenium  PageFactory, Annotation 实例. 先是类: HomePage package com.test;import org.openqa.selenium. ...

  5. selenium定位页面元素的一件趣事

    PS:本博客selenium分类不会记载selenium打开浏览器,定位元素,操作页面元素,切换到iframe,处理alter.confirm和prompt对话框这些在网上随处可见的信息:本博客此分类 ...

  6. Selenium操作页面元素

    转自:http://blog.sina.com.cn/s/blog_6966650401012a7q.html 一.输入框(text field or textarea) //找到输入框元素: Web ...

  7. python+selenium自动化软件测试(第6章):selenium phantomjs页面解析使用

    我们都知道Selenium是一个Web的自动化测试工具,可以在多平台下操作多种浏览器进行各种动作,比如运行浏览器,访问页面,点击按钮,提交表单,浏览器窗口调整,鼠标右键和拖放动作,下拉框和对话框处理等 ...

  8. 利用PIL和Selenium实现页面元素截图

    预备 照张相片 selenium.webdriver可以实现对显示页面的截图: from selenium import webdriver dr = webdriver.Firefox() dr.g ...

  9. Selenium 定位页面元素 以及总结页面常见的元素 以及总结用户常见的操作

    1. Selenium常见的定位页面元素 2.页面常见的元素 3. 用户常见的操作 1. Selenium常见的定位页面元素 driver.findElement(By.id());driver.fi ...

随机推荐

  1. Webpack2入门

    webpack 2 将在其文档完成之后正式发布.但这并不意味着不可以开始使用它,如果你知道怎么配置的话. 什么是 Webpack? 简单来说,Webpack 就是一个针对 JavaScript 代码的 ...

  2. js读取跨域webapi传送回来的cookie 要点

    1.webapi 返回cookie时,httpOnly=false 2.webapi 接收Origins 不能为* 3.js端 请求时,withCredentials必须: true   //`wit ...

  3. Spring知识点总结(二)之Spring IOC

    1.创建bean类,并在spring中进行配置交由spring来管理1. IOC(DI) - 控制反转(依赖注入)    所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管 ...

  4. css3 笔记

    1.元字符使用: []: 全部可选项 ||:并列 |:多选一 ?: 0个或者一个 *:0个或者多个 {}: 范围 2.CSS3属性选择器: E[attr]:存在attr属性即可: E[attr=val ...

  5. 没有上司的舞会(树形DP)

    题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...

  6. MySQL数据库常见报错原因

    1.启动数据库时报错 启动 # /etc/init.d/mysqld start Starting MySQL.Logging to '/application/mysql-5.6.36/data/m ...

  7. dynamic-insert和dynamic-update属性

    dynamic-insert  作用:设置对象中没有值的字段 insert并不会对其进行插入. 实体类映射配置如下 <!DOCTYPE hibernate-mapping PUBLIC &quo ...

  8. 浅谈localStorage的用法

    今天接到一个任务,说是让自动调节textarea标记的输入高度,而且还要记录下来,下次登录的时候还是调节后的高度,我第一时间就想到了localStorage的用法,直接代码献上: <html l ...

  9. 【rip-基础配置】

    配置rip,默认rip  id为 1:rip有version1和version2两个版本;宣告与rip直连的网段; 优化rip: [interface_name] rip poison-reverse ...

  10. js获取播放器播放时间和停止播放

    html代码 <video id="myVideo" class="video-active" width="100%" height ...