远程运行硒测试用例,用于使用chrome下载文件测试

时间:2019-05-15 13:42:29

标签: java selenium selenium-webdriver

我有一个测试,需要使用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();
    }

用于在本地和远程计算机上运行的测试的Suite基本代码

  //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.");

1 个答案:

答案 0 :(得分:0)

您可以在Chrome中手动设置自动下载文件,而无需设置很多大写字母和参数。接下来加载现有的浏览器配置文件。但是我没有远程尝试。

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();
    }
}