windows server 2003 怎么给网站做域名解析,网站建设模型,网站商城建设6,湖南省建设厅城乡建设网站我用selenium开发了一个自动创建任务#xff0c;大大解放了我做重复性工作带来的疲惫感#xff0c;收获了更多的乐趣。
我司有100多个服务#xff0c;运维忙不过来的时候#xff0c;就会让我们自己创建云负载#xff0c;你首先需要在云服务上创建负载#xff0c;再创建容…我用selenium开发了一个自动创建任务大大解放了我做重复性工作带来的疲惫感收获了更多的乐趣。
我司有100多个服务运维忙不过来的时候就会让我们自己创建云负载你首先需要在云服务上创建负载再创建容器配置好CPU、内存创建镜像跟容器的触发器创建Jenkins任务每个服务都需要这些步骤都是重复性工作枯燥乏味如果只有一个人弄那格式不可忍受。
为了能够让这件事更有趣点我使用了selenium来帮我做这些重复性工作。通过写到代码让其运行后就能自动打开浏览器填好指定文本提交表单下面来看看我是怎么用它来创建自动任务的关键是获取WEB的唯一的DOM节点。
介绍
先来看看Selenium的介绍没错它其实是一个自动化测试工具。
Selenium是一个用于Web应用程序测试的工具它直接运行在浏览器中可以模拟真正的用户操作。这个工具的主要功能包括测试与浏览器的兼容性即测试应用程序是否能够很好地工作在不同浏览器和操作系统之上和测试系统功能即创建回归测试检验软件功能和用户需求。Selenium支持自动录制动作和自动生成.Net、Java、Perl等不同语言的测试脚本。
Selenium主要包括三部分Selenium IDE、Selenium WebDriver和Selenium Grid。Selenium IDE是Firefox浏览器的一个插件可以进行录制回放并且可以把录制的操作以多种语言如JAVA、Python、C#等的形式导出成测试用例。Selenium WebDriver提供Web自动化所需的API主要用作浏览器控制、页面元素选择和调试。Selenium Grid则提供了在不同机器的不同浏览器上运行Selenium测试的能力。
Selenium支持跨不同浏览器、平台和编程语言的自动化它可以轻松部署在Windows、Linux、Solaris和Macintosh等平台上。此外Selenium通过使用特定于每种语言的驱动程序支持各种编程语言包括C#、Java、Perl、PHP、Python和Ruby等。测试脚本可以使用任何支持的编程语言进行编码并且可以直接在大多数现代Web浏览器中运行。
总的来说Selenium是一个功能强大的Web自动化测试工具它可以提高测试效率减少人工测试的工作量是Web开发人员和测试人员的重要工具之一。
下面是一个华为云创建负载的任务另外发现华为云的DOM节点ID、class加有随机数隔段时间就会变化因此我的例子不能直接拿去用要根据你自己的节点去修改你要掌握思路。
创建浏览器驱动
首先你要根据你自己的浏览器创建浏览器驱动如下我创建了一个Safari浏览器驱动。
依赖 dependencygroupIdorg.seleniumhq.selenium/groupIdartifactIdselenium-java/artifactIdversion4.1.1/version/dependency// 创建Safari浏览器驱动
WebDriver driver new SafariDriver();
//窗口大小
Dimension dimension new Dimension(1000, 1000);
driver.manage().window().setSize(dimension);登录
随后你要通过代码去登录华为云。 private static void login(WebDriver driver) throws InterruptedException {driver.get(https://auth.huaweicloud.com/authui/login.html?servicehttps://console.huaweicloud.com/cceold/#/login);Thread.sleep(3000);//切换IAM用户//你要找到你需要操作的DOM唯一标识WebElement iamLinkDiv driver.findElement(By.xpath(//*[id\IAMLinkDiv\]/span));((JavascriptExecutor) driver).executeScript(arguments[0].click();, iamLinkDiv);// 查找并填写用户名和密码表单点击提交按钮WebElement accountInput driver.findElement(By.id(IAMAccountInputId));WebElement usernameInput driver.findElement(By.id(IAMUsernameInputId));WebElement passwordInput driver.findElement(By.id(IAMPasswordInputId));WebElement submitButton driver.findElement(By.id(btn_submit));//点击一下相当于激活该选中框accountInput.click();//把需要填的内容发送到指定的文本accountInput.sendKeys(xxx);usernameInput.sendKeys(xxx);passwordInput.sendKeys(xxx);//执行JS登录因为有的表单是JS登录的并不是用的form表单((JavascriptExecutor) driver).executeScript(arguments[0].click();, submitButton);// 等待Jenkins页面加载完成Thread.sleep(5000);}创建任务
创建任务也就是负载你可以自定先声明要有哪些服务描述是什么等
private static void createJob(WebDriver driver, ListString services, ListString failList) {for (String service: services) {String[] serviceAry service.split(:);String serviceName serviceAry [0];String serviceDescription serviceAry [1];String serviceName ;try {serviceName serviceAry [2];} catch (Exception ex) {}try {System.out.println(String.format(-------正在创建任务[%s]......, serviceName));driver.get(https://console.huaweicloud.com/cce2.0/?agencyId09d4dbb7a300f3a61fc5c003d92470f3regioncn-east-3localezh-cn#/cce/cluster/fdd6c126-ebc2-11e9-bee9-0255ac1002c2/create/workload?namespacepaastypedeployment);Thread.sleep(3000);//通过唯一ID获取指定的DOM你看华为的ID后面都带了数字会变WebElement nameInput driver.findElement(By.id(ti_auto_id_13));WebElement instanceInput driver.findElement(By.id(ti_auto_id_15_input));WebElement containerInput driver.findElement(By.id(ti_auto_id_45));WebElement imageInput driver.findElement(By.id(ti_auto_id_46));WebElement cpuMaxInput driver.findElement(By.id(ti_auto_id_52));WebElement memoryMaxInput driver.findElement(By.id(ti_auto_id_55));WebElement subButton driver.findElement(By.id(ti_auto_id_237));nameInput.sendKeys(service- serviceName);instanceInput.clear();instanceInput.sendKeys(1);//时区同步选择//执行JS操作JS这个很好用有些不能直接操作的DOM都可以用JS去解决((JavascriptExecutor) driver).executeScript(document.getElementById(\ti_auto_id_20_awrapper\).click(););//清空输入框内容containerInput.clear();containerInput.sendKeys(service- serviceName);((JavascriptExecutor) driver).executeScript(document.getElementById(\ti_auto_id_47\).click(););ListWebElement imageSearchInput driver.findElements(By.className(ti3-searchbox-input));imageSearchInput.get(0).sendKeys(service- serviceName);//回车imageSearchInput.get(0).sendKeys(Keys.ENTER);ThreadUtil.sleep(3000);//选择镜像ListWebElement checkImage driver.findElements(By.className(ti3-radio-skin));((JavascriptExecutor) driver).executeScript(arguments[0].click();, checkImage.get(2));ListWebElement checkImageClick driver.findElements(By.className(ti3-btn-danger));((JavascriptExecutor) driver).executeScript(arguments[0].click();, checkImageClick.get(1));ThreadUtil.sleep(2000);cpuMaxInput.clear();cpuMaxInput.sendKeys(1);memoryMaxInput.clear();memoryMaxInput.sendKeys(1024);//提交((JavascriptExecutor) driver).executeScript(arguments[0].click();, subButton);// 等待创建任务完成Thread.sleep(5000);} catch (Exception ex) {ex.printStackTrace();System.out.println(失败的任务[] serviceName);//这里我将失败的任务保存起来等所有任务执行结束后控制台可以输入继续执行//因为由于页面加载时间是不确定的有时候会因为这个原因操作DOM失败failList.add(phantomService);}}}退出关闭
最后可以关闭浏览器
// 关闭浏览器
driver.quit();从我使用selenium的感受来说非常强大非常好用虽然刚开始写代码一遍遍测试有点花时间但当它自己跑起来你就可以喝喝茶看看它非常的有趣惬意。 官网地址https://www.selenium.dev/ 作者其他文章推荐 基于Spring Boot 3.1.0 系列文章 Spring Boot 源码阅读初始化环境搭建Spring Boot 框架整体启动流程详解Spring Boot 系统初始化器详解Spring Boot 监听器详解Spring Boot banner详解Spring Boot 属性配置解析Spring Boot 属性加载原理解析Spring Boot 异常报告器解析Spring Boot 3.x 自动配置详解