Thursday, December 29, 2016

IE Browser set preferences




How to run webdriver in IE browser?



package com.test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class TestIEBrowser { static String driverPath = "IE driver path"; public WebDriver driver; @BeforeClass public void setUp() { System.out.println("*******************"); System.out.println("launching IE browser"); System.setProperty("webdriver.ie.driver", driverPath+"IEDriverServer.exe"); driver = new InternetExplorerDriver(); driver.manage().window().maximize(); } @Test public void testGooglePageTitleInIEBrowser() { driver.navigate().to("http://www.google.com"); String strPageTitle = driver.getTitle(); System.out.println("Page title: - "+strPageTitle); Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match"); } @AfterClass public void tearDown() { if(driver!=null) { System.out.println("Closing IE browser"); driver.quit(); } } }


When ever working with Internet explorer browser for Selenium webdriver, the below are the common issues that you may come across.

1. If see issue some thing like 'Unexpected error launching Internet Explorer' below, You have to set 'Enable protected mode' option in all levels with same value.
org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 516 milliseconds
Please follow below steps to set:
1. Open Internet Explorer browser--> Select Internet Options from Tools menu
2. Select Security Tab --> Select Enable Protected Mode option -- > Check the default Zone level for 'Internet'. If you look at the screen shot below, security level for this zone is selected as 'Allowed level for this zone : Medium to High.' and 'Enable Protected Mode' option is Checked.

Now you need to make sure that, for the other Zones, such as 'Local Internet' and 'Trusted sites' is also selected as ABOVE. You may don't need to do anything with 'Restricted Site' option. We can leave the option as is and by default 'Enable Protected Mode' option will be Checked.
Now after changing the settings, please click on 'Apply' and 'Ok' button.
There is also an other alternative for setting the protected mode using desired capabilities as below: -

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer(); capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);





Google Chrome set preferences

String downloadFilepath = "/path/to/download";
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);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);

private static DesiredCapabilities getChromeCapabilities() throws Exception {

    String chromePath = BrowserUtil.class.getResource("/Browserdrivers/chromedriver.exe").getPath();
    System.setProperty("webdriver.chrome.driver", chromePath);
    String downloadFilepath = "C:\\TestDownloads";
    ChromeOptions options = new ChromeOptions();
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("download.default_directory", downloadFilepath);
    options.setExperimentalOption("prefs", chromePrefs);
    options.addArguments("--test-type");
    options.addArguments("start-maximized", "disable-popup-blocking");

    DesiredCapabilities chromeCapabilities = DesiredCapabilities.chrome();
    setProxy(chromeCapabilities);
    chromeCapabilities.setPlatform(Platform.WINDOWS);
    chromeCapabilities.setCapability("name", MDC.get("testname"));
    chromeCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
    return chromeCapabilities;
}

String downloadFilepath = "/path/to/download";
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);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);

Download file using selenium webdriver

As we know we cannot simulate OS actions with Selenium. We use AutoIt tool to upload documents (when it is not possible to achive upload using sendKeys method). We have discussed uploading a file using using Webdriver Sendkeys method and Using AutoIT Tool in earlier tutorials.
To handle Download functionality with selenium, we need to do some settings to the browser using Firefox profile using preferences, so that it automatically download the files to the defined folder. Then we can write code to check if the folder is downloaded or not.
If you want to download and save it to the desired location using Selenium Webdriver, then we need to set below Firefox profile preferences -
profile.setPreference("browser.download.dir", downloadPath);
Below is the example program to download a file
package com.pack;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;

public class FileDownloadExample {
 
 public static String downloadPath = "D:\\seleniumdownloads";
 @Test
 public void testDownload() throws Exception {
  WebDriver driver = new FirefoxDriver(FirefoxDriverProfile()); 
  driver.manage().window().maximize();
     driver.get("http://spreadsheetpage.com/index.php/file/C35/P10/");
     driver.findElement(By.linkText("smilechart.xls")).click();
 }
 
 public static FirefoxProfile FirefoxDriverProfile() throws Exception {
  FirefoxProfile profile = new FirefoxProfile();
  profile.setPreference("browser.download.folderList", 2);
  profile.setPreference("browser.download.manager.showWhenStarting", false);
  profile.setPreference("browser.download.dir", downloadPath);
  profile.setPreference("browser.helperApps.neverAsk.openFile",
    "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
  profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
  profile.setPreference("browser.helperApps.alwaysAsk.force", false);
  profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
  profile.setPreference("browser.download.manager.focusWhenStarting", false);
  profile.setPreference("browser.download.manager.useWindow", false);
  profile.setPreference("browser.download.manager.showAlertOnComplete", false);
  profile.setPreference("browser.download.manager.closeWhenDone", false);
  return profile;
 }
}
We will explain you the preferences that we have set to Firefox browser.
setPreference("browser.download.folderList", 2);
Default Value: 1
The value of browser.download.folderList can be set to either 0, 1, or 2. When set to 0, Firefox will save all files downloaded via the browser on the user's desktop. When set to 1, these downloads are stored in the Downloads folder. When set to 2, the location specified for the most recent download is utilized again.
setPreference("browser.download.manager.showWhenStarting", false);
Default Value: true
The browser.download.manager.showWhenStarting Preference in Firefox's about:config interface allows the user to specify whether or not the Download Manager window is displayed when a file download is initiated.
browser. helperApps. alwaysAsk. force
True: Always ask what to do with an unknown MIME type, and disable option to remember what to open it with False (default): Opposite of above
browser. helperApps. neverAsk. saveToDisk
A comma-separated list of MIME types to save to disk without asking what to use to open the file. Default value is an empty string.
browser. helperApps. neverAsk. openFile
A comma-separated list of MIME types to open directly without asking for confirmation. Default value is an empty string.
browser. download. dir
The last directory used for saving a file from the "What should (browser) do with this file?" dialog.
browser.download.manager.alertOnEXEOpen
True (default): warn the user attempting to open an executable from the Download Manager
False: display no warning and allow executable to be run
Note: In Firefox, this can be changed by checking the "Don't ask me this again" box when you encounter the alert.
browser. download. manager. closeWhenDone
True: Close the Download Manager when all downloads are complete
False (default): Opposite of the above
browser. download. manager. focusWhenStarting
True: Set the Download Manager window as active when starting a download
False (default): Leave the window in the background when starting a download
Note that you cannot implement this with Internet Explorer, as they don't use profiles. It's a limitation of the browser itself, not the IE driver. As such, there is no way to automatically download files to a specified location with Internet Explorer / Edge Browser.
Hope this helps you.

Thursday, December 22, 2016

sync merge process in SVN

10.96.82.195
srv-ind-ria2w\cafuser
c@fuser123


\\srv-ind-caf2.opentext.net\jenkins\caf-wip-sync-merge\caf-wip

srv-ind-caf2
root
support

cd /jenkins/workspace/caf-wip-sync-merge/caf-wip/

svn status | grep -P '^(?=.{0,6}C)' > /jenkins/workspace/caf-wip-sync-merge/conflicts.txt

Go to the respective component and resolve conflicts

Property conflicts: Resolve with local property

svn resolve -R --accept theirs-full /jenkins/workspace/caf-wip-sync-merge/caf-wip/components/cws/modelers

svn status --no-ignore | grep '^\?' | sed 's/^\?      //'

svn up

svn commit -F /jenkins/workspace/caf-wip-sync-merge/slavetools/merge-helper/mycomment.txt