分享

Selenium

 佬总图书馆 2019-12-18


  • AnndyTuo

    2018-07-22 

1:通常的分成设计模式

元素层+操作层+业务层

1.1:元素层

获取定位元素

1.2:操作层

对元素进行操作

1.3:业务层

传入参数 进行业务操作

2:PageObject登录的Demo演示

2.1:代码目录结构

2.2:代码实现

2.2.1:Base部分代码

SelectDriver.java

  1. package PageObject.Base;
  2. /**
  3. * Setup1:Base内的封装
  4. * */
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.chrome.ChromeDriver;
  7. import org.openqa.selenium.firefox.FirefoxDriver;
  8. /**
  9. * PageObject mode的
  10. *
  11. * 第一步:封装暖气的driver对象
  12. *
  13. * 作用:根据用户输入 选择不同的浏览器
  14. * */
  15. public class SelectDriver {
  16. //这里只默认firefox 和 chrome两种浏览器
  17. public WebDriver driverName(String browersName){
  18. if(browersName.equalsIgnoreCase('firefox')){
  19. return new FirefoxDriver();
  20. }else{
  21. return new ChromeDriver();
  22. }
  23. }
  24. }

DriverBase.java

  1. package PageObject.Base;
  2. /**
  3. * Setup1:Base内的封装
  4. * */
  5. import org.openqa.selenium.By;
  6. import org.openqa.selenium.WebDriver;
  7. import org.openqa.selenium.WebElement;
  8. /**
  9. * PageObject第二部: 封装driver
  10. *
  11. * 作用:产生driver对象
  12. * */
  13. public class DriverBase {
  14. private WebDriver driver;
  15. /**
  16. * 构造方法 创建对象时实例化driver
  17. * */
  18. public DriverBase(String browersName){
  19. SelectDriver selectDriver = new SelectDriver();
  20. //将SelectDriver中的driver对象赋值给'private WebDriver driver'中的driver 这样driver对象就有值了!!
  21. this.driver = selectDriver.driverName(browersName);
  22. }
  23. /**
  24. * 封装Element方法
  25. * */
  26. public WebElement element(By by){
  27. return driver.findElement(by);
  28. }
  29. /**
  30. * 封装get方法
  31. * */
  32. public void getUrl(String url){
  33. driver.get(url);
  34. }
  35. }

BasePage.java

  1. package PageObject.Base;
  2. /**
  3. * Setup1:Base内的封装
  4. * */
  5. import org.openqa.selenium.By;
  6. import org.openqa.selenium.WebElement;
  7. /**
  8. * PageObject第三步:BasePage的封装
  9. *
  10. * */
  11. public class BasePage {
  12. private DriverBase driver;
  13. /**
  14. * 构造方法
  15. * */
  16. public BasePage(DriverBase driver){
  17. this.driver = driver;
  18. }
  19. /**
  20. * 封装Element
  21. * */
  22. public WebElement element(By by){
  23. return driver.element(by);
  24. }
  25. /**
  26. * 封装click操作
  27. * */
  28. public void click(WebElement element){
  29. //判断element是否为空 null和非null的处理方式
  30. if(element != null){
  31. element.click();
  32. }else{
  33. System.out.println('你要点击的元素不存在');
  34. }
  35. }
  36. /**
  37. * 封装SendKeys操作
  38. * */
  39. public void sendKeys(WebElement element,String context){
  40. //判断element是否为空 null和非null的处理方式
  41. if(element != null){
  42. element.clear();
  43. element.sendKeys(context);
  44. }else{
  45. System.out.println('你要输入的元素不存在 输入内容失败');
  46. }
  47. }
  48. }

2.2.2:Utils

ReadProperties.java

  1. package PageObject.Utils;
  2. import java.io.BufferedInputStream;
  3. import java.io.FileInputStream;
  4. import java.util.Properties;
  5. public class ReadProperties {
  6. private String filePath = 'LoginElement.properties';
  7. private Properties properties;
  8. /**
  9. * 构造方法 创建对象时自动返回pro对象 在new该对象的时候会自动加载readProperties()方法
  10. * */
  11. public ReadProperties(String filePath){
  12. this.filePath = filePath;
  13. //在new该对象的时候会自动加载readProperties()方法
  14. this.properties = readProperties();
  15. }
  16. /**
  17. * 返回已经加载properties文件的pro对象
  18. * */
  19. public Properties readProperties(){
  20. //创建对象
  21. Properties pro = new Properties();
  22. //读取properties文件到缓存
  23. try {
  24. BufferedInputStream in = new BufferedInputStream(new FileInputStream(filePath));
  25. //加载缓存到pro对象 prop.load(in)这么写 不能读取properties配置文件中的中文
  26. pro.load(new InputStreamReader(in, 'utf-8'));
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. //返回pro对象
  31. return pro;
  32. }
  33. /**
  34. * 使用全局的properties对象获取key对应的value值
  35. * @return
  36. * */
  37. public String getValue(String key){
  38. return properties.getProperty(key);
  39. }
  40. }

2.2.3:元素层

ByMethon.java

  1. package PageObject.page;
  2. import org.openqa.selenium.By;
  3. import PageObject.Utils.ReadProperties;
  4. public class ByMethon {
  5. /**
  6. * 封装by 静态方法 直接调用
  7. * @throws IOException
  8. * */
  9. public static By bystr(String PropertiesKey){
  10. String PropertiesPath = 'LoginElement.properties';
  11. //创建ReadProperties对象
  12. ReadProperties properties = new ReadProperties(PropertiesPath);
  13. String value = properties.getValue(PropertiesKey);
  14. //对value进行拆分
  15. String LocateMethon = value.split('>')[0];
  16. String LocateEle = value.split('>')[1];
  17. //System.out.println(LocateMethon+'========='+LocateEle);
  18. if(LocateMethon.equalsIgnoreCase('id')){
  19. return By.id(LocateEle);
  20. }else if(LocateMethon.equalsIgnoreCase('name')){
  21. return By.name(LocateEle);
  22. }else if(LocateMethon.equalsIgnoreCase('tagNmae')){
  23. return By.tagName(LocateEle);
  24. }else if(LocateMethon.equalsIgnoreCase('linkText')){
  25. return By.linkText(LocateEle);
  26. }else if(LocateMethon.equalsIgnoreCase('xpath')){
  27. return By.xpath(LocateEle);
  28. }else if(LocateMethon.equalsIgnoreCase('cssSelector')){
  29. return By.cssSelector(LocateEle);
  30. }else{
  31. return By.partialLinkText(LocateEle);
  32. }
  33. }
  34. }

loginPage.java

  1. package PageObject.page;
  2. import org.openqa.selenium.WebElement;
  3. import PageObject.Base.BasePage;
  4. import PageObject.Base.DriverBase;
  5. public class loginPage extends BasePage{
  6. /**
  7. * 构造方法
  8. * */
  9. public loginPage(DriverBase driver) {
  10. super(driver);
  11. }
  12. /**
  13. * 获取登录按钮的element
  14. * super类BasePage有封装一个element方法 此处element可以直接使用
  15. * */
  16. public WebElement findLoginButtion(){
  17. //return super.element(ByMethon.bystr('userCountBox'));
  18. return element(ByMethon.bystr('userCountBox'));
  19. }
  20. /**
  21. * 获取'账号登录'的位置的element
  22. * */
  23. public WebElement checkCountLogin(){
  24. return element(ByMethon.bystr('countLogin'));
  25. }
  26. /**
  27. * 获取登录框的element
  28. * */
  29. public WebElement loginBox(){
  30. return element(ByMethon.bystr('loginUser'));
  31. }
  32. /**
  33. * 获取密码框的element
  34. * */
  35. public WebElement passwordElement(){
  36. return element(ByMethon.bystr('loginPassword'));
  37. }
  38. /**
  39. * 获取登录按钮的element
  40. * */
  41. public WebElement loginButtion(){
  42. return element(ByMethon.bystr('loginButtion'));
  43. }
  44. }

2.2.4:操作层

loginPageHandle.java

  1. package PageObject.handle;
  2. /**
  3. * 操作层
  4. * */
  5. import PageObject.Base.DriverBase;
  6. import PageObject.page.loginPage;
  7. /**
  8. * 封装登录页面的操作 操作loginPage下的方法
  9. * */
  10. public class loginPageHandle {
  11. private DriverBase driver;
  12. private loginPage loginpage;
  13. /**
  14. * 构造方法
  15. * */
  16. public loginPageHandle(DriverBase driver){
  17. this.driver = driver;
  18. //loginpage放在这里的目的是使用driver,保证driver的一致性
  19. loginpage = new loginPage(driver);
  20. }
  21. /**
  22. * 封装寻找登录框按钮的操作
  23. * */
  24. public void findLoginButtion(){
  25. loginpage.click(loginpage.findLoginButtion());
  26. }
  27. /**
  28. * 封装点击账号登录的方法
  29. * */
  30. public void countLogin(){
  31. loginpage.click(loginpage.checkCountLogin());;
  32. }
  33. /**
  34. * 封装登录框的操作
  35. * */
  36. public void userLogin(String username){
  37. loginpage.sendKeys(loginpage.loginBox(), username);
  38. }
  39. /**
  40. * 封装密码输入
  41. * */
  42. public void passwordLogin(String password){
  43. loginpage.sendKeys(loginpage.passwordElement(), password);
  44. }
  45. /**
  46. * 封装点击登录的操作
  47. * */
  48. public void loginAction(){
  49. loginpage.click(loginpage.loginButtion());
  50. }
  51. }

2.2.5:业务层

loginBuss.java

  1. package PageObject.busession;
  2. /**
  3. * 业务层
  4. * */
  5. import PageObject.Base.DriverBase;
  6. import PageObject.handle.loginPageHandle;
  7. public class loginBuss {
  8. private loginPageHandle loginPgeHandle;
  9. /**
  10. * 构造方法 实例化loginPageHandle
  11. * */
  12. public loginBuss(DriverBase driver){
  13. loginPgeHandle = new loginPageHandle(driver);
  14. }
  15. /**
  16. * 执行操作业务
  17. * */
  18. public void bussLogin(String username,String password){
  19. loginPgeHandle.findLoginButtion();
  20. loginPgeHandle.countLogin();
  21. loginPgeHandle.userLogin(username);
  22. loginPgeHandle.passwordLogin(password);
  23. loginPgeHandle.loginAction();
  24. }
  25. }

2.2.6:执行用例

CaseBase.java

  1. package PageObject.Case;
  2. import PageObject.Base.DriverBase;
  3. /**
  4. * 生成driver对象 所有的case都集成这个类
  5. * */
  6. public class CaseBase {
  7. /**
  8. * 初始化driver
  9. * */
  10. public DriverBase initDriver(String browers){
  11. DriverBase driver = new DriverBase(browers);
  12. return driver;
  13. }
  14. }

loginCase.java

  1. package PageObject.Case;
  2. import org.junit.Test;
  3. import PageObject.Base.DriverBase;
  4. import PageObject.Utils.ReadProperties;
  5. import PageObject.busession.loginBuss;
  6. /**
  7. * 测试登录操作
  8. * */
  9. public class loginCase extends CaseBase{
  10. private DriverBase driver;
  11. private loginBuss loginBuss;
  12. /**
  13. * 构造方法初始化loginBuss
  14. * */
  15. public loginCase(){
  16. this.driver = initDriver('firefox');
  17. loginBuss = new loginBuss(driver);
  18. }
  19. @Test
  20. public void LoginTest() throws Exception{
  21. //读取配置文件
  22. ReadProperties readProperties = new ReadProperties('LoginElement.properties');
  23. //这个driver来自于DriverBase DriverBase类下封装了gerUrl方法
  24. String URL = readProperties.getValue('URL');
  25. driver.getUrl(URL);
  26. Thread.sleep(3000);
  27. //获取username 和 password
  28. String username = readProperties.getValue('LoginInfo').split('>')[0];
  29. String password = readProperties.getValue('LoginInfo').split('>')[1];
  30. loginBuss.bussLogin(username, password);
  31. }
  32. }


2.2.7:配置文件

LoginElement.properties

  1. #URL
  2. URL=https://www.csdn.net/
  3. #用户名和密码
  4. LoginInfo=你的账户>你的密码
  5. #页面获取登录框的位置
  6. userCountBox=xpath>.//*[@id='csdn-toolbar']/div/div/ul/li[5]/a[1]
  7. #获取'账号登录'的位置
  8. countLogin=xpath>html/body/div[3]/div/div/div[2]/div/h3/a
  9. #输入账号的位置
  10. loginUser=id>username
  11. #输入密码框的位置
  12. loginPassword=xpath>.//*[@id='password']
  13. #登录按钮的位置
  14. loginButtion=xpath>.//*[@id='fm1']/input[8]
文章最后发布于: 2018-07-22
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多