引言
Selenium WebDriver是一个流行的自动化测试框架,可用于自动化Web应用程序的测试。它允许测试人员与浏览器交互,就像真实用户一样。本教程将指导您如何使用Selenium WebDriver自动执行网站登录流程。
先决条件
在开始之前,您需要确保以下先决条件已满足:
- 已安装Java开发工具包(JDK)
- 已安装Selenium WebDriver
- 已安装您要自动化的网站
步骤
1. 创建Selenium项目
mkdir my-selenium-project cd my-selenium-project mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart
2. 添加Selenium依赖项
org.seleniumhq.selenium selenium-java 4.1.1
3. 编写Selenium测试用例
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import java.sql.; import java.util.List;public class LoginTest {public static void main(String[] args) throws SQLException {// 设置ChromeDriver路径System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");// 创建WebDriver实例WebDriver driver = new ChromeDriver();// 打开登录页面driver.get("https://example.com/login");// 随机从数据库中获取用户名和密码Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "password");Statement stmt =conn.createStatement();ResultSet rs = stmt.executeQuery("SELECT FROM users ORDER BY RAND() LIMIT 1");String username = rs.getString("username");String password = rs.getString("password");// 填写登录表单driver.findElement(By.name("username")).sendKeys(username);driver.findElement(By.name("password")).sendKeys(password);// 提交登录表单driver.findElement(By.id("submit-button")).click();// 断言登录是否成功ListsuccessMessage = driver.findElements(By.cssSelector(".success-message"));if (successMessage.size() > 0) {System.out.println("登录成功");} else {System.out.println("登录失败");}// 关闭WebDriver实例driver.quit();} }
4. 运行测试用例
mvn test
结论
通过遵循本教程中的步骤,您应该能够使用Selenium WebDriver自动执行网站登录流程。这是一种强大的技术,可用于节省时间和精力,并提高Web应用程序测试的效率。
怎么用python做自动化测试?
这里以web自动化测试为例,简单介绍一下如何使用python进行web自动化测试,主要用到selenium这个框架,实验环境win10+python3.6,主要内容如下:
1.首先,安装selenium框架,这个直接在cmd窗口输入命令“pipinstallselenium”就行,如下,安装非常快:
2.安装完成后,还需要安装浏览器驱动程序,不然直接运行程序会报错,以谷歌浏览器chrome为例,需要下载chromedriver驱动程序,如下,这里chromedriver的版本必须要与自己平台浏览器的版本匹配:
下载完成后,是一个zip压缩包,里面就一个文件,这里需要将这个文件复制到python安装目录下,如下:
3.最后,我们就可以进行selenium框架测试了,测试代码如下,非常简单,创建一个webdriver,如果能正常打开对应网页,则说明selenium安装成功:
之后就可以直接定位相关元素,进行web自动化测试了,主要方法如下(共有8种),分别是id、name、classname、tagname、linktext、partiallinktext、xpath和cssselector,这里可以自行测试,相关资料非常丰富:
至此,我们就完成了pythonweb自动化测试框架selenium的安装和简单使用。总的来说,整个过程非常简单,只要你有一定的python基础,熟悉一下上面的安装过程,很快就能搭建好本地selenium自动化测试框架,网上也有相关教程和资料,介绍的非常详细,感兴趣的话,可以搜一下,希望以上分享的内容能对你有所帮助吧,也欢迎大家评论、留言进行补充。
1、编写Python版本的minicom,这个是自动化测试日志记录的需要用于控制串口输出的字符颜色,高亮显示出错信息,方便开发者在线调试问题2、控制程控电源、程控开关,这些可以自动化测试的硬件基础3、写整套测试框架,控制整个测试交互流程,Case管理等等Python运行起来效率没那么高,但是对于编写来说是非常高效的。
Selenium自动化时滑块验证处理
Selenium中对滑块的操作基本是采用元素拖拽的方式,而这种方式需要用到Selenium的Actionchains功能模块的drag_and_drop_by_offset方法 drag_and_drop_by_offset(source,xoffset,yoffset) source:鼠标拖动的原始元素 xoffset:鼠标把元素拖动到另一个位置的x坐标 yoffset:鼠标把元素拖动到另一个位置的y坐标思路 :1. 获取滑块元素,求出滑块的长度和宽度获取滑块区域元素,求出其长度和宽度2. 使用drag_and_drop_by_offset方法移动滑块到滑块区域的最大长度及宽度以携程网登录页面举例 实现携程网站的登录功能 from seleniumimport webdriver import time from _chainsimport ActionChains driver = (C:/Users/Administrator/PycharmProjects/untitled/) _wait(30) (_window() # 点击登录 _element_by_css_selector(#nav-bar-set-login > a > span)() # 登录账号 account =123 _element_by_id(nloginname)_keys(account) # 登录密码 password =*** _element_by_id(npwd)_keys(password) # 拖动滑块 slider = _element_by_css_selector(#sliderddnormal > -drop-box > -drop-btn) # print([width])方便理解,打印出来40px # print([height]) 打印出来40px slider_area = _element_by_css_selector(#sliderddnormal > -drop-box > -bg-bar) # print(slider_[width])打印出来288px # print(slider_[height]) 打印出来40pxActionChains(driver)_and_drop_by_offset(slider,slider_[width],[height])() (3)
WebDriver到底怎么用
1.2 用webdriver打开一个浏览器我们常用的浏览器有firefox和IE两种,firefox是selenium支持得比较成熟的浏览器。
但是做页面的测试,速度通常很慢,严重影响持续集成的速度,这个时候建议使用HtmlUnit,不过HtmlUnitDirver运行时是看不到界面的,对调试就不方便了。
使用哪种浏览器,可以做成配置项,根据需要灵活配置。
打开firefox浏览器://Create a newinstance of the Firefox driverWebDriver driver = newFirefoxDriver();打开IE浏览器//Create a newinstance of the Internet Explorer driverWebDriver driver = newInternetExplorerDriver ();打开HtmlUnit浏览器//Createa new instance of the Internet Explorer driverWebDriverdriver = new HtmlUnitDriver();1.3 打开测试页面对页面对测试,首先要打开被测试页面的地址(如:),web driver 提供的get方法可以打开一个页面:// And now use thedriver to visit (GettingStartedpackage ;import ;import ;import ;import ;import ;import ;public class Selenium2Example {public static voidmain(String[] args) {// Create a newinstance of the Firefox driver// Notice that theremainder of the code relies on the interface,// not the driver = newFirefoxDriver();// And now use this tovisit Google (Alternatively thesame thing can be done like this// ()(Find the text inputelement by its nameWebElement element =((q));// Enter something tosearch for (Cheese!);// Now submit the will find the form for us from the ();// Check the title ofthe page (Page title is: + ());// Googles search isrendered dynamically with JavaScript.// Wait for the pageto load, timeout after 10 seconds(newWebDriverWait(driver, 10))(new ExpectedCondition<Boolean>() { public Booleanapply(WebDriver d) {()()(cheese!);}});// Should see:cheese! – Google Search (Page title is: + ());//Close the ();}}第2章Webdirver对浏览器的支持2.1 HtmlUnit Driver优点:HtmlUnit Driver不会实际打开浏览器,运行速度很快。
对于用FireFox等浏览器来做测试的自动化测试用例,运行速度通常很慢,HtmlUnit Driver无疑是可以很好地解决这个问题。
缺点:它对JavaScript的支持不够好,当页面上有复杂JavaScript时,经常会捕获不到页面元素。
使用:WebDriver driver = new HtmlUnitDriver();2.2 FireFox Driver优点:FireFox Dirver对页面的自动化测试支持得比较好,很直观地模拟页面的操作,对JavaScript的支持也非常完善,基本上页面上做的所有操作FireFox Driver都可以模拟。
缺点:启动很慢,运行也比较慢,不过,启动之后Webdriver的操作速度虽然不快但还是可以接受的,建议不要频繁启停FireFox Driver。
使用:WebDriver driver = new FirefoxDriver();Firefox profile的属性值是可以改变的,比如我们平时使用得非常频繁的改变useragent的功能,可以这样修改:FirefoxProfile profile = new FirefoxProfile();(, some UAstring);WebDriver driver = new FirefoxDriver(profile);2.3 InternetExplorer Driver优点:直观地模拟用户的实际操作,对JavaScript提供完善的支持。
缺点:是所有浏览器中运行速度最慢的,并且只能在Windows下运行,对CSS以及XPATH的支持也不够好。
使用:WebDriver driver = new InternetExplorerDriver();第3章使用操作3.1如何找到页面元素Webdriver的findElement方法可以用来找到页面的某个元素,最常用的方法是用id和name查找。
下面介绍几种比较常用的方法。
3.1.1 By ID假设页面写成这样:<input type=textid=passwd-id />那么可以这样找到页面的元素:通过id查找:WebElement element = ((passwd-id));3.1.2 By Name或通过name查找:WebElement element = ((passwd));3.1.3 By XPATH或通过xpath查找:WebElement element =((//input[@id=passwd-id]));3.1.4 By Class Name假设页面写成这样:<div class=cheese><span>Cheddar</span></div><divclass=cheese><span>Gouda</span></div>可以通过这样查找页面元素:List<WebElement>cheeses = ((cheese));3.1.5 By Link Text假设页面元素写成这样:<ahref=那么可以通过这样查找:WebElement cheese =((cheese));3.2如何对页面元素进行操作找到页面元素后,怎样对页面进行操作呢?我们可以根据不同的类型的元素来进行一一说明。
3.2.1 输入框(text field or textarea) 找到输入框元素:WebElement element = ((passwd-id));在输入框中输入内容(“test”);将输入框清空();获取输入框的文本内容();3.2.2 下拉选择框(Select)找到下拉选择框的元素:Select select = new Select(((select))); 选择对应的选择项(“mediaAgencyA”);或(“MA_ID_001”);不选择对应的选择项();(“MA_ID_001”);(“mediaAgencyA”);或者获取选择项的值();();3.2.3 单选项(Radio Button)找到单选框元素:WebElement bookMode =((BookMode));选择某个单选项();清空某个单选项();判断某个单选项是否已经被选择();3.2.4 多选项(checkbox)多选项的操作和单选的差不多:WebElement checkbox =((myCheckbox.));();();();();3.2.5 按钮(button)找到按钮元素:WebElement saveButton = ((save));点击按钮();判断按钮是否 ();3.2.6 左右选择框也就是左边是可供选择项,选择后移动到右边的框中,反之亦然。
例如:Select lang = new Select(((languages)));(“English”);WebElement addLanguage =((addButton));();3.2.7 弹出对话框(Popup dialogs)Alert alert = ()();();();();3.2.8 表单(Form)Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:WebElement approve = ((approve));();或();//只适合于表单的提交3.2.9 上传文件 (Upload File)上传文件的元素操作:WebElement adFileUpload = ((WAP-upload));String filePath = C:\test\\uploadfile\\media_ads\\;(filePath);3.2.10 Windows 和 Frames之间的切换一般来说,登录后建议是先()();切换到某个frame()(leftFrame);从一个frame切换到另一个frame()(mainFrame);切换到某个window()(windowName);3.2.11 拖拉(Drag andDrop)WebElement element =((source));WebElement target = ((target));(new Actions(driver))(element, target)();3.2.12 导航 (Navigationand History)打开一个新的页面: ()(通过历史导航返回原页面()();()();
【搜客吧】_激活码商城_微商营销货源站 » 使用Selenium WebDriver自动执行网站登录流程 (使用SELECT语句随机地从表中挑出指定数量的行)
常见问题FAQ
- 本站资源可以代理扶持吗?有没有售后和技术支持?
- 具体请参照:https://www.wxzygp.com/345.html
- 遇到问题无法解决怎么办?
- 免费下载或者VIP会员资源能否直接商用?
- 购买该资源后,可以退款吗?