微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

selenium grid自动化测试全过程

记录搭建和踩坑全程,便于以后参考

服务器准备

本机和服务器互ping
1、本机ping不通服务器
ping不通服务器的排查方法

解决方法

添加安全组规则后解决

2、服务器ping不通本机
本机WiFi是局域网(内网),只能ping通公网ip,暂未解决

jar包

下载地址:http://selenium-release.storage.googleapis.com/index.html

下载版本:selenium-server-standalone-2.53.1.jar

将此jar包放在主机和子机上

主机

进入服务器上jar包位置,启动

nohup java -jar selenium-server-standalone-2.53.1.jar -role hub -maxSession 10 -port 4444 &

参数解析

l -role hub表示启动运行hub;

l -port是设置端口号,hub的认端口是4444,这里使用的是认的端口,当然可以自己配置;

l -maxSession为最大会话请求,这个参数主要要用并发执行测试用例,认是1,建议设置10及以上。

浏览器打开地址:http://IP:4444/grid/console

在这里插入图片描述

节点

在本地启动命令

java -jar "D:\\Code\\tool\\jar\\selenium-server-standalone-2.53.1.jar" -role node -port 6666 -hub http://192.168.1.50:4444/grid/register -Dwebdriver.chrome.driver="D:\\Development Tools\\Chrome\\chromedriver.exe" -maxSession 5 -browser browserName=chrome,seleniumProtocol=WebDriver,maxInstances=5,platform=WINDOWS

注:提供jar包路径、node机ip、驱动路径

控制台:

在这里插入图片描述

网页:(网页加载可能很慢)

在这里插入图片描述

经过测试,本机电脑是内网ip,服务器无法ping通内网ip,此问题待解决,后续换用同WiFi下的另一台电脑进行后续操作

建Java项目,执行以下代码

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * @author 
 * 2021-02-25 15:45
 */
public class WebDriverTest {
    public static void main(String[] args)  {
        WebDriverTest wb = new WebDrivertest();
        wb.chrome();
    }

    private void chrome(){
        try {
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            capabilities.setbrowserName("chrome");
            capabilities.setVersion("88");
            capabilities.setPlatform(Platform.WINDOWS);
            //System.setProperty("webdriver.chrome.driver","D:\\Development Tools\\Chrome\\chromedriver.exe");
            //System.setProperty("webdriver.chrome.bin","C:\\Program Files (x86)\\Google\\Chrome\\Application");
            WebDriver driver = new RemoteWebDriver(new URL("http://172.18.4.109:6666/wd/hub"),capabilities);
            driver.get("https://owntest.gcycloud.cn/portal/index.html");
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            runScript(driver);
            //driver.close();
        } catch (Exception e){
            e.printstacktrace();
        }
    }

    private void runScript(WebDriver driver) {
        try {
            WebElement keyword=driver.findElement(By.xpath("//*[@id=\"userAccount\"]"));
            keyword.sendKeys("username");
            System.out.println("username");
            keyword = driver.findElement(By.xpath("//*[@id=\"password\"]"));
            keyword.sendKeys("password");
            System.out.println("password");
            Thread.sleep(2000);
            keyword = driver.findElement(By.xpath("//button[@id=\"user_login\"]"));
            keyword.click();

            System.out.println("login success");
            Thread.sleep(2000);
            keyword = driver.findElement(By.xpath("//span[text()=\"采购监管\"]"));
            keyword.click();
            System.out.println("span success");
            Thread.sleep(2000);
            keyword = driver.findElement(By.xpath("//*[@class=\"sidebar-item-icon iconfont-portal icon-portal-yiji-anquanjianguan\"]"));
            keyword.click();
            System.out.println("* success");
        } catch (Exception e){
            e.printstacktrace();
            System.out.println("catch exception");
        }
    }
}

node机上启动浏览器执行,远程链接控制成功

以上过程参考了此链接

补充:
可以用json文件配置,上面流程中没用,后续完善

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐