• java 启动Selenium 以及端口占用的问题




    ERROR [BaseServer.start] - Port 4444 is busy, please choose a free port and specify it using -port..


    开启Selenium server

    问题描述
    例如:使用命令java -jar .\src\main\resources\driver\selenium-server-standalone-3.141.59.jar出现报错如下:


    原因分析:
    显示端口4444被占用,解决方法要么停掉端口,要么换成其他端口

     

    解决方案:
    我这边采用的是第2种方法,切换为8090端口
    命令:java -jar .\src\main\resources\driver\selenium-server-standalone-3.141.59.jar -port 4445
    开启Selenium server成功!

     

    Java运行web自动化代码1

    package com.test.demo.web;

    import com.test.pages.SearchPages;
    import com.test.utils.Config;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;

    import java.util.Properties;

    /**
     * @Author: xinyi
     * @Description:
     * @Date: Created In 9:11 2018/8/3
     * @Modified By:
     */
    public class TestDemo {
        WebDriver driver;

        public static void main(String[] args) {
    //        test();
        }

    //    @Test
    //    public static void test(){
    //        String url = "http://www.baidu.com";
    //        WebDriver driver;
    //        //新建一个浏览器句柄
    //        driver = new Brower().chrome();
    //        //打开URL
    //        driver.get(url);
    //        //输入搜索字符串
    //        driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("pYTHON");
    //        //点击[百度一下]按钮
    //        driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
    //        try {
    //            Thread.sleep(2000);
    //        } catch (InterruptedException e) {
    //            e.printStackTrace();
    //        }
    //    }

        @BeforeTest
        public void init() {
            String url = "http://www.baidu.com";

            //新建一个浏览器句柄
            driver = new Brower().chrome();
            //打开URL
            driver.get(url);
        }

        @Test
        public void testSearch(){
            Browser browser = new Browser();
            Properties properties = Config.getProperties();
            WebDriver driver = browser.openUrl(properties.getProperty("url"));
            SearchPages search = new SearchPages();
            search.input(driver, "12306");
            search.search(driver);
            driver.close();
        }
        @Test
        @Parameters("searchWord")
        public void search(String searchWord) throws InterruptedException {
            //输入搜索字符串
            System.out.println("searchWord: "+searchWord);
            driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys(searchWord);
            //点击[百度一下]按钮
            driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
            Thread.sleep(2000);
        }

        @AfterTest
        public void teardown() {
            driver.quit();
        }

    }
     

    Java运行web自动化代码2

    package com.test.demo.web;
    
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.testng.annotations.Test;
    
    import java.util.Properties;
    
    /**
     * @author 作者:微信搜索关注【测试工程师成长之路】公众号
     * @version 创建时间:2020/12/5
     * 类说明:打开浏览器
     */
    public class OpenBrowser {
    
        public WebDriver driver;
    
        @Test
        public void test() throws InterruptedException {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--kiosk");//MAC OS下浏览器最大化
            Properties props = System.getProperties();
            String systemOS = props.getProperty("os.name");
            if(systemOS.contains("Mac")){
                // 设置驱动属性,驱动存放在drivers路径下
                System.setProperty("webdriver.chrome.driver", "drivers/chromedriver");
                driver = new ChromeDriver(options);
            }else{
                System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
                driver = new ChromeDriver();
            }
            // Chrome浏览器打开cnblogs
            driver.get("https://www.cnblogs.com/mrjade");
            Thread.sleep(3000);
            if (!"Mac".contains(systemOS)) {
                // win下浏览器最大化
                driver.manage().window().maximize();
            }
            // 关闭浏览器
            driver.close();
            // 关闭浏览器驱动
            driver.quit();
        }
    
    }
    
    
  • 相关阅读:
    【计算机毕业设计】微信小程序:MHK自学平台的设计与实现——后附源码
    Jmeter——性能测试的认知以及思考bug(一)
    Hotspot启动原理(一)
    GIT使用踩坑!!!我的妈妈呀,烦死了烦死了烦死了!!!
    Nacos注册中心
    用HTML+CSS做一个漂亮简单的轻量级图片相册博客网站(web前端期末大作业)
    【Python编程】教你如何打造极致体验的五子棋对战游戏
    34.【C/C++ string大汇总,不看定后悔】
    基于PLC控制四自由度气动式机械手设计
    基于深度学习的多曝光图像融合 - MATLAB 实现
  • 原文地址:https://blog.csdn.net/qq_30273575/article/details/127412783