简体   繁体   中英

Remote run selenium test cases for download file test using chrome

I have a test which I need to run on a remote machine using selenium /chrome/ java. When I run same test cases on local it works fine there is no error or anything it pass. when I run same test cases on remote machine after click on download file test freezer up or will keep counting there is no error or anything.

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

code for Suite base for test to run on local and remote machine

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

Instead of setting lots of caps and args you can set up manually in Chrome to automatically download files. Following with loading existing browser profile. However I didn't try remotely.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM