当前位置: 首页 > news >正文

网站建设模板下载免费百度新闻搜索

网站建设模板下载免费,百度新闻搜索,镇江怎么样,做排行榜的网站1. 准备 编程语言:Java JDK版本:17 Maven版本:3.6.1 2. 开始 声明:本次只测试Java的Selenium自动化功能 本次示例过程:打开谷歌游览器,进入目标网址,找到网页的输入框元素,输入指…

1. 准备

编程语言:Java
JDK版本:17
Maven版本:3.6.1

2. 开始

声明:本次只测试Java的Selenium自动化功能
本次示例过程:打开谷歌游览器,进入目标网址,找到网页的输入框元素,输入指定内容,点击提交按钮,成功后关闭网页。

2.1. 目录结构和内容

在这里插入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.3.2</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.fhh.selenium</groupId><artifactId>demo</artifactId><version>1.0.0</version><name>SeleniumDemo</name><description>Selenium demo</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version><selenium.version>4.23.1</selenium.version><webdrivermanager.version>5.9.2</webdrivermanager.version></properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>${selenium.version}</version></dependency><!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager --><!--            https://github.com/bonigarcia/webdrivermanager--><dependency><groupId>io.github.bonigarcia</groupId><artifactId>webdrivermanager</artifactId><version>${webdrivermanager.version}</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>
</project>

SimpleSelenium.java

/*** Copyright (C) 2024-2024, FHH. All rights reserved.*/
package com.fhh.selenium;import lombok.AllArgsConstructor;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;import java.util.function.Consumer;/*** SimpleSelenium** @apiNote <a href="https://www.selenium.dev/zh-cn/documentation/overview/">文档详情</a>* @since 1.0.0*/
@AllArgsConstructor
public class SimpleSelenium {private final WebDriver driver;public SimpleSelenium openWeb() {driver.get("https://www.selenium.dev/selenium/web/web-form.html");return this;}public SimpleSelenium getWebTitle(Consumer<? super String> action) {action.accept(driver.getTitle());return this;}public SimpleSelenium fillForm() {WebElement textBox = driver.findElement(By.name("my-text"));// 设置textBox的值为“Test selenium”textBox.clear();textBox.sendKeys("Test selenium");return this;}public SimpleSelenium submit() {WebElement submitButton = driver.findElement(By.cssSelector("button"));submitButton.click();return this;}public SimpleSelenium getMessage(Consumer<? super String> action) {WebElement message = driver.findElement(By.id("message"));action.accept(message.getText());return this;}public void quit() {driver.quit();}
}

UserAction.java

封装用户操作的示例

/*** Copyright (C) 2024-2024, FHH. All rights reserved.*/
package com.fhh.selenium;import lombok.AllArgsConstructor;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;/*** 封装一些用户的必要动作** @since 1.0.0*/
@AllArgsConstructor
public class UserAction {private final WebDriver driver;/*** 登录用户** @param username 用户名* @param password 密码* @return this*/public UserAction login(String username, String password) {WebElement loginField = driver.findElement(By.id("username"));loginField.clear();loginField.sendKeys(username);// Fill out the password field. The locator we're using is "By.id", and we should// have it defined elsewhere in the class.WebElement passwordField = driver.findElement(By.id("password"));passwordField.clear();passwordField.sendKeys(password);// Click the login button, which happens to have the id "submit".driver.findElement(By.id("submit")).click();return this;}/*** 注销登录** @return this*/public UserAction logOut() {WebElement logOutButton = driver.findElement(By.id("log-out"));logOutButton.click();return this;}}

SimpleSeleniumTest.java

/*** Copyright (C) 2024-2024, FHH. All rights reserved.*/
package com.fhh.selenium;import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;import java.time.Duration;import static org.junit.jupiter.api.Assertions.assertEquals;class SimpleSeleniumTest {WebDriver driver;@BeforeEachpublic void setup() {driver = new ChromeDriver();}@Testpublic void simpleTest() {// 1. 打开网页driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));driver.get("https://www.selenium.dev/selenium/web/web-form.html");// 2. 获取网页标题,并断言为Web formString title = driver.getTitle();assertEquals("Web form", title);// 3. 找到网页上name属性为“my-text”的元素textBoxWebElement textBox = driver.findElement(By.name("my-text"));// 3.1 设置textBox的值为SeleniumtextBox.sendKeys("Test selenium");//        sleep(2000);// 4. 点击提交元素WebElement submitButton = driver.findElement(By.cssSelector("button"));submitButton.click();//        sleep(2000);// 5. 断言页面名称为“Web form - target page”assertEquals("Web form - target page", driver.getTitle());//        sleep(2000);// 6. 找到id为“message”的消息,断言为“Received!”WebElement message = driver.findElement(By.id("message"));assertEquals("Received!", message.getText());}@Testpublic void simpleSeleniumTest() {new SimpleSelenium(driver).openWeb().getWebTitle(title -> assertEquals("Web form", title)).fillForm().submit().getWebTitle(title -> assertEquals("Web form - target page", title)).getMessage(message -> assertEquals("Received!", message)).quit();}@AfterEachpublic void teardown() {driver.quit();}public void sleep(long timeMillis) {try {Thread.sleep(timeMillis);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}

3. 效果

运行测试后,会先等待1~5秒(具体看机器性能),打开一个谷歌游览器,在输入网址,进入网页,并找到网页中的元素,输入内容后,点击提交按钮,网页会自动跳转到新的页面。结束

部分代码和页面关系

测试用例结果
测试用例结果


如果对你有用,就点个赞吧~多谢

http://www.khdw.cn/news/14257.html

相关文章:

  • 广州哪个大学做网站制作好些的域名注册服务网站哪个好
  • 哪些网站可以找兼职做室内设计如何做好搜索引擎优化工作
  • 茶叶网站建设网页设计制作上海网站制作公司
  • 百度优化网站建设快速seo整站优化排行
  • 内蒙古 网站建设长春网络优化最好的公司
  • 网站开发公司资讯seo营销推广全程实例
  • 手机微信网站怎么做ks刷粉网站推广马上刷
  • 大型网站建设兴田德润优惠长沙网站优化效果
  • 南京网站建设一条龙seo搜索引擎优化试题及答案
  • 如何开通免费网站网络培训网站
  • PS做任务的网站windows优化大师的优点
  • 丰润区建设局网站互联网推广方案怎么写
  • 泰兴做网站的公司成人英语培训
  • 家庭做网站自己做网络推广怎么做
  • 久久业务自助下单平台手机seo快速排名
  • 租用微信做拍卖网站视频号视频下载助手app
  • 如何做一个动态网站电子商务营销策略有哪些
  • 泉州快速优化排名seo技巧与技术
  • 大兴网站开发网站建设哪家好北京公司排名seo
  • 深圳光明新区网站建设宁波网站推广代运营
  • 岳阳做网站多少钱在线识别图片找原图
  • 百度优化网站建设整合营销策略有哪些
  • 佛山做公司网站湖人队最新消息
  • 网站开发委托协议书范本挖掘关键词工具
  • 河北抖音seo系统seo外链工具有用吗
  • 网站切换语言怎么做的产品营销方案策划书
  • 网站咨询弹窗怎么做百度seo软件
  • 关于计算机网站建设的论文最快的新闻发布平台
  • 广州模板网站什么是引流推广
  • 杭州网站公司天津seo托管