【问题标题】:Remote run selenium test cases for download file test using chrome使用 chrome 远程运行 selenium 测试用例以进行下载文件测试
【发布时间】:2019-05-15 22:17:28
【问题描述】:

我有一个测试需要使用 selenium /chrome/java 在远程机器上运行。当我在本地运行相同的测试用例时,它工作正常,没有错误或任何通过的东西。当我点击下载文件测试冷冻机后在远程机器上运行相同的测试用例或将继续计数时没有错误或任何东西。

 //Click on download file icon     
   click(driver, downloadIcon, format + " icon " + summaryReportName);
// after click test freezer or will count keep i++
    try {
 // file location on network path location same same local run and remote run 
    long beforeCount = Files.list(Paths.get("//ap-521-6be1/Selenium/Onetest/excelfiles")).count();
        System.out.println(beforeCount);
        long afterCount = beforeCount;
        int i = 1;
        while (beforeCount >= afterCount) {
            Thread.sleep(1000);
            afterCount = Files.list(Paths.get("//ap-521-6be1/Selenium/Onetest/excelfiles")).count();
     // will continue printing the count i++ without any error 
            //System.out.println(i++);
            i++;
        }
        System.out.println("Time took to download report:" +i+" seconds");
    } catch (IOException e) {
        Add_Log.info("Excel report not downloaded for Exception");
        e.printStackTrace();            
    }
    Thread.sleep(6000);
    File theNewestFile = null;      
    File dir1 = new File("//ap-521-6be1/Selenium/Onetest/excelfiles");  

    File[] files = dir1.listFiles();
    if (files == null || files.length == 0) {
        return;
    }

    File lastModifiedFile = files[0];

String filename1 = lastModifiedFile.getName();

    System.out.println(filename1+ " & " + summaryReportName);
    if (filename1.contains(summaryReportName)) {
        Add_Log.info("Successfully downloaded ");
        Reporter.log("Successfully downloaded ");
    } else {
        Add_Log.info(" not downloaded");
        Reporter.log(" not downloaded");

        Assert.fail();
    }

用于测试在本地和远程机器上运行的套件基础代码

  //To Load Chrome driver Instance.

        /*System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\src\\main\\resources\\browserdrivers\\chromedriver.exe");
    //  String downloadFilepath = System.getProperty("user.dir")+"\\src\\main\\resources\\excelfiles\\";
        String downloadFilepath ="//ap-521-6be1/Selenium/Onetest/excelfiles";
        HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("profile.default_content_settings.popups", 0);
        chromePrefs.put("download.default_directory", downloadFilepath);
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", chromePrefs);
        options.addArguments("--start-maximized");
        options.setExperimentalOption("useAutomationExtension", false);
        options.addArguments("disable-infobars");
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        cap.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);

        cap.setCapability(ChromeOptions.CAPABILITY, options);
        driver.set(new ChromeDriver(cap));
        Add_Log.info("Chrome Driver Instance loaded successfully.");
        */


//for remote run
         DesiredCapabilities capability = DesiredCapabilities.chrome();
           ChromeOptions options = new ChromeOptions();
            System.setProperty("webdriver.chrome.driver", ("user.dir")+"\\src\\main\\resources\\browserdrivers\\chromedriver.exe");
            String downloadFilepath ="//ap-521-6be1/Selenium/Onetest/excelfiles";
            options.setExperimentalOption("useAutomationExtension", false);
         //   driver = new ChromeDriver(options);
        // ChromeOptions options = new ChromeOptions();
        options.addArguments("--test-type");

        options.addArguments("disable-infobars");
        options.addArguments("--start-maximized");
        options.setExperimentalOption("useAutomationExtension", false);
        capability.setBrowserName("chrome");
        capability.setPlatform(Platform.WINDOWS);
        capability.setCapability(ChromeOptions.CAPABILITY, options);                        
        try {
            driver.set(new RemoteWebDriver(new URL("http://146.76.184.178:4455/wd/hub"), capability));
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //For Remote run end
        Add_Log.info("Chrome Driver Instance loaded successfully.");

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    您可以在 Chrome 中手动设置以自动下载文件,而不是设置大量的 caps 和 args。接下来加载现有的浏览器配置文件。但是我没有远程尝试。

    package packageName;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    public class WebdriverSetup {   
        public static String chromedriverPath = "C:\\Users\\pburgr\\Desktop\\selenium-tests\\GCH_driver\\chromedriver.exe";
    
        // my default profile folder
        public static String chromeProfilePath = "C:\\Users\\pburgr\\AppData\\Local\\Google\\Chrome\\User Data";    
    
        public static WebDriver driver; 
        public static WebDriver startChromeWithCustomProfile() {
            System.setProperty("webdriver.chrome.driver", chromedriverPath);
            ChromeOptions options = new ChromeOptions();
    
            // loading Chrome with my existing profile instead of a temporary profile
            options.addArguments("user-data-dir=" + chromeProfilePath);
    
            driver = new ChromeDriver(options);
            driver.manage().window().maximize();
            return driver;
        }
        public static void shutdownChrome() {
            driver.close();
            driver.quit();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-11
      • 2012-11-23
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      相关资源
      最近更新 更多