基于Java/Python搭建Web UI自动化环境

Java搭建UI自动化测试环境

下载JDK8

https://www.cnblogs.com/thloveyl/p/12378124.html

配置Java环境

1.解压Jdk压缩包
3rpnte.png
2.配置环境变量
计算机->属性->高级->环境变量->系统变量->Path
3rpZTO.png
3.添加根目录下的bin与lib目录、jre下的bin目录(近期我发现只将bin目录加入Path就可以了)
3rpF61.png
{% asset_img 2.PNG %}
注:都是目录下绝对路径地址,我这儿在E盘。
4.输入java、javac、java -version确认
3rpufH.png

下载selenium server

Selenium官网
Selenium Jar包

安装对应版本的Chrome浏览器驱动

点击下载chromedriver
3rp9fJ.png

IDEA导入jar包

File->Project Settings->Modules->Dependencies->'+'->JARS...
3rpkOx.png
选中之后点ok->勾上jar包->applay->ok

实例

package com.smeoa.UI;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chrome.ChromeDriver;
public class DemoChromeDriver
{
    public static void main(String[] args) throws Exception
    {
        //设置浏览器的驱动属性、值。
        System.setProperty("webdriver.chrome.driver","D:\\Driver\\chromedriver.exe");
        //取消 chrome正受到自动测试软件的控制的信息栏
        ChromeOptions options = new ChromeOptions();
        options.addArguments("disable-infobars");
        //实例化Driver对象
        WebDriver driver = new ChromeDriver(options);
        //打开网址:在url栏输入https://www.baidu.com
        driver.get("http://www.baidu.com");
        //xpath元素定位:输入栏,sendKeys输入Java
        driver.findElement(By.xpath("/html//input[@id='kw']")).sendKeys("github");
        //点击百度一下
        driver.findElement(By.xpath("/html//input[@id='su']")).click();
        Thread.sleep(100);
        //关闭浏览器
        driver.close();
    }
}

Python搭建UI自动化环境

下载Python3

环境配置

  • 安装Python

  • 勾选Add Python to PATH,一直下一步。

  • 验证:CMD输入Python

    3rpzut.png

下载Chrome Driver

安装PyCharm

转载:安装教程

安装Selenium

  • 打开PyCharm
  • 新建Python File
  • 点击面板底部 Terminal
    3r9SDP.png
  • 输入pip install selenium 安装、pip list 验证
    3rpjgA.png

实例

from selenium import webdriver

# 设置 :忽略正在受自动测试软件的控制
option = webdriver.ChromeOptions()
option.add_argument('disable-infobars')
# 实例化 对象
driver = webdriver.Chrome(chrome_options=option)
# 打开网址
driver.get("https://www.baidu.com")
# 通过id元素定位到输入框,输入github
driver.find_element_by_id("kw").send_keys("github")
# 点击百度一下
driver.find_element_by_id("su").click()
driver.close()

猜你喜欢

转载自www.cnblogs.com/thloveyl/p/12378637.html