web自动化框架—BasePage 类的简单封装

BasePage类:

Copy
import com.ggf.selenium.taxcp.utils.BrowserUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.Set;

/**

  • @Description: 页面基础类,将公共方法提取出来,如:获取元素内容,点击,等待。。。

  • @Author: ggf

  • @Date: 2021/05/15
    /
    public class BasePage {
    /
    *

    • 对元素进行点击
    • @param by 定位信息
      */
      public void click(By by) {
      waitElementClickable(by).click();
      }

    /**

    • 对元素输入内容
    • @param by 定位信息
    • @param datas 输入的内容
      */
      public void sendKeys(By by, String datas) {
      waitElementVisible(by).sendKeys(datas);
      }

    /**

    • 清除元素内容
    • @param by
      */
      public void clear(By by) {
      waitElementVisible(by).clear();
      }

    /**

    • 判断元素是否存在。
    • 使用isDisplayed() 方法,该方法判断某个元素是否在页面中存在,包含了:
    • visibility=hidden 或 display=none 时,只要在HTML代码中存在,
    • 就会返回true。
    • 扩展:
    • isEnable()–>用于判断input、select等元素是否可编辑。
    • isSelected()–>判断元素是否被选中。
    • @param by 定位信息
    • @return 存在:true 不存在:false
      */
      public boolean isElementExist(By by) {
      return waitElementVisible(by).isDisplayed();
      }

    /**

    • 获取元素文本内容
    • @param by 定位信息
    • @return 返回文本内容
      */
      public String getElementText(By by) {
      return waitElementVisible(by).getText();
      }

    /**

    • 获取元素属性值
    • @param by 定位信息
    • @param attributeName 属性名称
    • @return 返回属性值
      */
      public String getElementAttributeValue(By by, String attributeName) {
      return waitElementVisible(by).getAttribute(attributeName);
      }

    /**

    • 等待元素可见,显式等待10秒。
    • @param by 定位信息
    • @return 返回element对象
      */
      public WebElement waitElementVisible(By by) {
      WebDriverWait webDriverWait = new WebDriverWait(BrowserUtil.getDriver(),10);
      return webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(by));
      }

    /**

    • 等待元素可点击,显式等待10秒。
    • @param by 定位信息
    • @return 返回element对象
      */
      public WebElement waitElementClickable(By by) {
      WebDriverWait webDriverWait = new WebDriverWait(BrowserUtil.getDriver(), 10);
      return webDriverWait.until(ExpectedConditions.elementToBeClickable(by));
      }

    /**

    • 等待iframe可见并且根据By切换到iframe框架中
    • @param by 元素定位信息
      */
      public void waitIframeAndSwitch(By by) {
      WebDriverWait webDriverWait = new WebDriverWait(BrowserUtil.getDriver(), 10);
      webDriverWait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by));
      }

    /**

    • 等待iframe可见并且根据名称切换。
    • @param frameName iframe名称
      */
      public void waitIframeAndSwitch(String frameName) {
      WebDriverWait webDriverWait = new WebDriverWait(BrowserUtil.getDriver(), 10);
      webDriverWait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameName));
      }

    /**

    • 滑动列表找元素并且进行点击

    • @param selectedText 选中元素文本

    • @param by 正在加载类似元素的定位表达式

    • @throws InterruptedException
      */
      public static void clickElementInList(String selectedText, By by) throws InterruptedException {
      // 滑动之前的页面源代码信息
      String beforeSource = “”;
      // 滑动之后的页面源代码信息
      String afterSource = “”;
      // 获取webdriver
      WebDriver driver = BrowserUtil.getDriver();

      // 循环条件
      // 1、找到了元素,跳出循环
      // 2、如果没有找到元素???怎么跳出循环
      while (true) {
      WebElement webElement = driver.findElement(by);
      beforeSource = driver.getPageSource();
      JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
      javascriptExecutor.executeScript(“arguments[0].scrollIntoView(0);”, webElement);
      // 如果当前页面有想要找的元素,怎么判断是否有??–getPageSource
      if (driver.getPageSource().contains(selectedText)) {
      driver.findElement(By.linkText(selectedText)).click();
      break;
      }
      Thread.sleep(1000);
      afterSource = driver.getPageSource();
      // 页面元素没有变化—滑动到了最底部
      if (afterSource.equals(beforeSource)) {
      break;
      }
      Thread.sleep(1500);
      }
      }

    /**

    • 滚动到指定元素上的方法
    • @param by 定位信息
      */
      public void scrollIntoElement(By by){
      WebElement webElement= waitElementVisible(by);
      JavascriptExecutor javascriptExecutor = (JavascriptExecutor) BrowserUtil.getDriver();
      javascriptExecutor.executeScript(“arguments[0].scrollIntoView(0);”, webElement);
      }

    /**

    • 根据title切换窗口的方法

    • @param title 窗口的标题
      */
      public void switchWindowByTitle(String title){
      WebDriver driver = BrowserUtil.getDriver();
      Set handles = driver.getWindowHandles();
      // 切换窗口的方式–循环遍历handles集合
      for (String handle : handles) {
      //判断是哪一个页面的句柄??–根据什么来判断???title
      if(driver.getTitle().equals(title)){
      break;
      }else{
      //切换窗口–根据窗口标识来切换
      driver.switchTo().window(handle);
      }

      }
      }

    /**

    • 根据url内容切换窗口的方法

    • @param urlFlag 窗口的标题
      */
      public void switchWindowByUrl(String urlFlag){
      WebDriver driver = BrowserUtil.getDriver();
      Set handles = driver.getWindowHandles();

      for (String handle : handles) {
      //根据url内容判断句柄
      if(driver.getCurrentUrl().contains(urlFlag)){
      break;
      }else{
      //切换窗口
      driver.switchTo().window(handle);
      }

      }
      }
      }

BrowserUtil类:

Copy
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

/**

  • @Description: 浏览器工具类,控制浏览器的生命周期,浏览器启动,关闭

  • @Author: ggf

  • @Date: 2021/05/15
    */
    public class BrowserUtil {

    /**
    *为解决多线程问题,通过ThreadLocal机制来控制
    */
    private static ThreadLocal threadLocal = new ThreadLocal();

    /**

    • 通过传入不同的浏览器名称,获取浏览器驱动
    • @param driverType 不同的浏览器 chrome ie Firefox
      */
      public static void setDriverType(String driverType) {
      // 判断输入内容是否为空。
      if (driverType != “” && driverType != null) {
      // 创建不同的浏览器驱动
      if (“chrome”.equalsIgnoreCase(driverType)) {
      ChromeDriver chromeDriver = new ChromeDriver();
      setDriver(chromeDriver);
      }else if (“ie”.equalsIgnoreCase(driverType)) {
      InternetExplorerDriver ieDriver = new InternetExplorerDriver();
      setDriver(ieDriver);
      }else if (“firefox”.equalsIgnoreCase(driverType)) {
      FirefoxDriver fireDriver = new FirefoxDriver();
      setDriver(fireDriver);
      }
      }
      }

    /**

    • 从线程ThreadLocal中获取对象
    • @return
      */
      public static WebDriver getDriver() {
      return threadLocal.get();
      }

    /**

    • 设置对象到ThreadLocal中
    • @param driver
      */
      public static void setDriver(WebDriver driver) {
      threadLocal.set(driver);
      }

    /**

    • 关闭浏览器
      */
      public void closeBrowser() {
      getDriver().close();
      }
      }

USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以了解到一个WEB UI自动化测试框架的三个核心模块:对象层、逻辑层和业务层。下面是一个简单WEB UI自动化测试框架搭建封装的步骤: 1. 安装Python和Selenium库 ```shell pip install selenium ``` 2. 创建一个基础BasePage,用于封装一些基础方法,例如元素定位、点击、输入等操作。 ```python from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By class BasePage(object): def __init__(self, driver): self.driver = driver def find_element(self, locator, timeout=10): element = WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator)) return element def click(self, locator): element = self.find_element(locator) element.click() def input_text(self, locator, text): element = self.find_element(locator) element.clear() element.send_keys(text) ``` 3. 创建一个页面,继承BasePage,并实现该页面的元素定位和控件操作方法。 ```python from selenium.webdriver.common.by import By from base_page import BasePage class LoginPage(BasePage): # 元素定位 username_input = (By.ID, 'username') password_input = (By.ID, 'password') login_button = (By.ID, 'login-button') # 控件操作方法 def input_username(self, username): self.input_text(self.username_input, username) def input_password(self, password): self.input_text(self.password_input, password) def click_login_button(self): self.click(self.login_button) ``` 4. 创建一个逻辑层,用于实现一些功能用例的模块。 ```python from page.login_page import LoginPage class LoginAction(object): def __init__(self, driver): self.driver = driver self.login_page = LoginPage(self.driver) def login(self, username, password): self.login_page.input_username(username) self.login_page.input_password(password) self.login_page.click_login_button() ``` 5. 创建一个业务层,用于实现测试用例的操作部分。 ```python from selenium import webdriver from action.login_action import LoginAction class TestLogin(object): def setup_method(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get('http://www.example.com') def teardown_method(self): self.driver.quit() def test_login(self): login_action = LoginAction(self.driver) login_action.login('username', 'password') # 添加断言等测试用例操作 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值