Selenium+Java+Chrome进行web自动化实例

发布时间 2023-04-29 10:27:38作者: shuzihua
Selenium+Java+Chrome进行web自动化实例 
 
 
这是我第一次在项目中使用Java Spring启动,因为我主要使用C#,我需要从blob URL路径读取文件并将一些字符串数据(如密钥)附加到同一个文件中。在我的API下载文件之前流。
 
以下是我尝试过的方法:
 
FileOutputStream / InputStream:抛出FileNotfoundException,因为它无法解析blob路径。
 
URLConnection:这让我到了某个地方,我能够成功下载文件但是当我在下载之前尝试写入/附加一些值时,我失败了。
 
我一直在做的代码。
 
//EXTERNAL_FILE_PATH is the azure storage path ending with for e.g. *.txt
 
URL urlPath = new URL(EXTERNAL_FILE_PATH);
 
URLConnection connection = urlPath.openConnection();
 
connection.setDoOutput(true); //I am doing this as I need to append some data and the docs mention to set this flag to true.
 
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
 
out.write("I have added this");
 
out.close();
 
//this is where the issues exists as the error throws saying it cannot read data as the output is set to true and it can only write and no read operation is allowed. So, I get a 405, Method not allowed...
 
inputStream = connection.getInputStream();
 
我不确定框架是否允许我修改URL路径中的某个文件并同时读取并下载相同的文件
 
2018-04-10 11:56:19
本文用一个简单的例子来说明从环境搭建到实现web自动化的整个过程,让初学者快速入门。网上很多文章可能只介绍过程中的某一部分,初学者往往一头雾水,而本文则是详细介绍,根据本文一步一步能成功执行自动化测试。
 
一、环境搭建
1. Eclipse
2. Chrome及对应的Chromedriver
网上搜索的很多映射都是几年前的,扎心!最新的映射如下:
 
映射(全)请参考:
 
https://chromedriver.storage.googleapis.com/2.37/notes.txt
1
1
3.查看chrome版本,下载对应的Chromedriver,配置环境变量
 
1)下载chromedriver
2)将WebDriver复制到Chrome的安装目录
安装目录可以通过在Chrome地址栏中输入chrome://version/来查看
3)将WebDriver的路径复制到系统环境变量PATH中
即将C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe添加到PATH中
1
2
3
4
5
1
2
3
4
5
二、自动化测试实例
1.新建Maven Project,在pom.xml加入dependency
 
 
org.seleniumhq.selenium
selenium-java
3.0.0
 
 
org.testng
testng
6.14.2
test
 
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
需要注意的是,保存后一般下载失败!设置一下镜像下载地址即可,步骤如下:
1)新建一个settings文件
 
 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
 
 
alimaven
aliyun maven
http://maven.aliyun.com/nexus/content/groups/public/
central
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
2)Eclipse>>Window>>Preferences>>Maven>>User Settings:
在右侧的User Settings下选择该文件
 
点击ok,此时再保存pom.xml会下载成功
 
2.新建TestNG class,编写脚本
 
功能:打开浏览器,进入百度,输入CSDN,点击搜索,关闭浏览器
1
1
package com.bond.SeleniumProj;
 
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
 
public class WebAutoTest {
public WebDriver driver;
String baseUrl = "http://www.baidu.com/";
@Test
public void testBaiduSearch() {
driver.get(baseUrl+"/");
WebElement inputBox = driver.findElement(By.id("kw"));
Assert.assertTrue(inputBox.isDisplayed());
inputBox.sendKeys("CSDN");
driver.findElement(By.id("su")).click();
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver = new ChromeDriver();
}
 
@AfterMethod
public void afterMethod() {
driver.quit();
}
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
当然,代码中的findElement等元素定位,将在以后的章节进行介绍,敬请期待~~
 
3.Run as >>TestNG Test
 
 
控制台输出:
 
Starting ChromeDriver 2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7) on port 11747
Only local connections are allowed.
四月 10, 2018 10:21:23 上午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Attempting bi-dialect session, assuming Postel’s Law holds true on the remote end
四月 10, 2018 10:21:25 上午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: OSS
PASSED: testBaiduSearch
 
===============================================
Default test
 
===============================================
Tests run: 1, Failures: 0, Skips: 0
 
===============================================
Default suite Total tests run: 1, Failures: 0, Skips: 0
 
同时,可以看到浏览器自动打开,并搜索CSDN,浏览器关闭。
 
打开CSDN,阅读体验更佳
 
Java+selenium简单实现web自动化测试
最近在转行软件测试,有点艰难,为了提高自己的竞争力,只有不断的学习,学习多一点技术和知识。 这次要学习的是Java+Selenium的Web自动化测试,虽然这个自动化测试技术已经少有人用,或者说已经没人用了,很多相关的插件都不支持更新了,而且所用的到selenium相关jar包的版本必须要和firefox浏览器版本相匹配才能真正跑起来的。 这个需要学习的就不用担心,文末会提供匹配的jar包和F...
浏览器打开
Selenium Java(1)Chrome进行web自动化实例的配置
环境配置 个人使用浏览器版本(Google Chrome ): 74.0.3729.131 (正式版本) (64 位) 驱动版本:74.0.3729.6 [chromedriver_win32.zip ] 亲测可行 下载chromedriver驱动 不同的浏览器版本下载不同驱动版本.下载地址 配置环境变量(win10) 将下载的驱动复制到Chrome的安装目录,安装目录可通过在Chrome地...
浏览器打开
 
评论(1)
写评论
 
Tpraise码龄2年
 
感谢,对我非常有用6 月前
相关推荐
selenium webdriver 启动三大浏览器Firefox,Chrome,IE
安装三大浏览器驱动driver 1.chromedriver 下载地址:https://code.google.com/p/chromedriver/downloads/list 2.Firefox的驱动geckodriver 下载地址:https://github.com/mozilla/geckodriver/releases/ 3.IE的驱动IEdriver 下载地址:http://www.nuget.org/packages/Selenium.WebDriver.
浏览器打开
【selenium3+JAVA】界面自动化测试教程(一)——浏览器的启动之chrome浏览器的启动
一、环境 本机安装jdk1.8 本机安装eclipse eclipse添加maven组件 新建maven工程并在POM.xml中添加如下字段: <dependency> <groupId>org.seleniumhq.selenium</groupId&gt
浏览器打开
selenium+Java+IDEA集成自动化测试
selenium是一个应用于web应用程序测试的框架,可以模拟终端客户在实际运用中的场景。本篇博客将介绍selenium的基本测试API,自动化截图,生成测试报告等功能。 具体博客流程如下: selenium基本属性介绍 selenium确定界面元素的几种方法 selenium自动截图 selenium生成测试报告 1.selenium基本属性介绍 selenium运...
浏览器打开
Java爬虫第一篇:准备 chromedriver与chrome
chromedriver与chrome各版本及下载地址 记录下使用Selenium时,遇到的chromedriver版本问题。 概述 我们做自动化的时候如果用的是selenium,首先要解决的就是下载不同浏览器的驱动,目前的浏览器驱动都是由各大浏览器厂商自己更新提供。 运行selenium自动化脚本报错如下: org.openqa.selenium.WebDriverException: unkn...
浏览器打开
 
Java + selenium 实现web自动化简单示例
环境搭建: eclipse4.5 + java8 + selenium-3.14 基本都是使用最新版 (1)eclipse4.5下载:http://www.eclipse.org/downloads/ (2)java8官方网站下载:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.h...
浏览器打开
java的webdriver_Java + selenium 实现web自动化简单示例
importjava.util.Iterator;importjava.util.List;importjava.util.Set;importjava.util.concurrent.TimeUnit;importorg.openqa.selenium.Alert;importorg.openqa.selenium.By;importorg.openqa.selenium.Cookie;impo...
浏览器打开
Java+Selenium3.0基础篇1-环境搭建
一.背景和目的        我大概是2015年开始接触Selenium这个web自动化测试框架。当时是selenium2.48好像,从Selenium2.0开始,就加入了webdriver,实际上,我们说的selenium自动化测试,大部分情况都是在使用webdriver的API。现在去Selenium官网,发现最新Selenium版本是3.4,我也不知道3.4和2.48之间有什么多大的区别和
浏览器打开
Java - selenium - Chrome 自动化测试例子
代码如下: 类 MainTest.java: import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver.Window; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOp...
浏览器打开