使用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. 【转】不错的linux下通用的java程序启动脚本

    虽然写起动shell的频率非常不高...但是每次要写都要对付一大堆的jar文件路径,新加jar包也必须要修改起动shell. 在网上找到一个挺好的通用shell脚本. 只需要修改一些配置变量,就可以用 ...

  2. Linq 集合比较

    List<RemindTbl> l_lstRemind = (from x in RemindTbl where (from y in RemindTblOld where x.Atten ...

  3. context.RewritePath

    context.RewritePath原理修改HttpRequest类中的Path属性

  4. 复合词(Compound Words, UVa 10391)(stl set)

    You are to find all the two-word compound words in a dictionary. A two-word compound word is a word i ...

  5. 解决:fontawesome-webfont.woff2?v=4.6.3 404 (Not Found)

    用Bootstrap里面的字体,你项目中会报一个错,一个字体找不到,但我们的项目中却是存在这个字体的. 解决方法: 修改我们的Web.Config文件

  6. C#添加二维码带加密带logo

    #region 生成QR码,加密与logo在此处修改 public static void CreateQr(string strQrContent, DataTable myTable) { Qr ...

  7. Web中的中文参数乱码

    中文参数乱码    1 get方式传参,中文乱码        修改tomcat中的配置server.xml        在修改端口的标签中添加属性URIEncoding="XXX&quo ...

  8. leetcode笔记(五)809. Expressive Words

    题目描述 Sometimes people repeat letters to represent extra feeling, such as "hello" -> &qu ...

  9. 【杂题总汇】HDU-5215 Cycle

    ◆HDU-5215◆ Cycle 国庆节集训的第三天……讲图论,心情愉快……刷了一堆水题,不过也刷了一些有意思的题 +传送门+ HDU ▶ 题目 给出一个无向图(无自环,无重边),求该无向图中是否存在 ...

  10. 使用Python对Csv文件操作

    csv是Comma-Separated Values的缩写,是用文本文件形式储存的表格数据,比如如下的表格: 就可以存储为csv文件,文件内容是: No.,Name,Age,Score 1,mayi, ...