Friday, January 8, 2016

Selenium Internet Explorer Driver usage

Available Constructors for InternetExplorerDriver




  • Use of internet explorer constructor "InternetExplorerDriver(capabilities)"



   // using Internet Explorer local through Internet Explorer Driver  
      public static void ieLocalDriver(WebDriver driver) {  
           System.setProperty("webdriver.ie.driver",  
                     "C:\\Java_Source_Code\\IEDriverServer_Win32_2.48.0\\IEDriverServer.exe");  
           DesiredCapabilities capabilities = new DesiredCapabilities();  
           // setting IE specific capability  
           // this is to override Protected mode settings for zones  
           capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);  
           // this is for setting logging  
           capabilities.setCapability(InternetExplorerDriver.LOG_FILE, "C:\\Test\\ie_log\\logs.log");  
           // this is for setting logging level  
           capabilities.setCapability("logLevel", "INFO");  
           driver = new InternetExplorerDriver(capabilities);  
  
      }  


  • Use of  set capability

capability can be either set through InternetExplorerDriver class static fields OR can be set through static final fields from CapabilityType Interface.


  • Using InternetExplorerDriver constructor  "InternetExplorerDriver(service, capabilities)"


      public static void ieLocalDriver02(WebDriver driver) {  
           System.setProperty("webdriver.ie.driver",  
                     "C:\\Java_Source_Code\\IEDriverServer_Win32_2.48.0\\IEDriverServer.exe");  
           // this is to override Protected mode settings for zones  
           DesiredCapabilities capabilities = new DesiredCapabilities();  
           capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);  
           InternetExplorerDriverService.Builder builder = new InternetExplorerDriverService.Builder();  
           InternetExplorerDriverService service = builder.withLogFile(new File("C:\\Test\\ie_log\\ie.log"))  
                     .withLogLevel(InternetExplorerDriverLogLevel.INFO).build();  
           // I am using the capability and service both  
           driver = new InternetExplorerDriver(service, capabilities);  
      }  


Internet Explorer specific capability 
https://code.google.com/p/selenium/wiki/DesiredCapabilities

This is how the capability constructor are used



For setting up the capability of a browser either you use STATIC FINAL VALUES of 
InternetExplorerDriver



OR 
pass it as a String parameter to capabilities.setCapability(String, String value), etc

Other Internet Explorer Capabilities


   

Thursday, January 7, 2016

TestNG printing data provider parameter to report and Reporter Interface



Test NG. Use of Reporter.Log, Data Provider, Getting the data provider parameter on reports




      @DataProvider(name = "dp")  
      public String[][] dp() {  
           String testCaseName[][] = { { "T_Name01"},{"T_Name02" }};  
           return testCaseName;  
      }  
      String testCaseNamePrint = "Test_Case_01";  
      WebDriver driver;  
      @Test(enabled = true, dataProvider = "dp", dataProviderClass = sudas.com.se.study.SandBoxTest.class)  
      public void testCase01(String testCaseNamePrint) {  
           driver.navigate().to("http://www.google.co.in");  
 Reporter.log("This is my Test", true);

      }  
      @Test  
      public void testCase02() {  
           System.out.println("Test Case 02");  
      }  
      @Test  
      public void testCase03() {  
           System.out.println("Test Case 03");  
      }  
      @Test  
      public void testCase04() {  
           System.out.println("Test Case 04");  
      }  
      @BeforeMethod  
      public void beforeMethod() {  
           System.out.println("@BeforeMethod");  
      }  
      @AfterMethod  
      public void afterMethod() {  
           System.out.println("@AfterMethod");  
      }  
      @BeforeClass  
      public void beforeClass() {  
           System.out.println("@BeforeClass");  
      }  
      @AfterClass  
      public void afterClass() {  
           System.out.println("@AfterClass");  
      }  
      @BeforeTest  
      public void beforeTest() {  
           System.out.println("@BeforeTest");  
      }  
      @AfterTest  
      public void afterTest() {  
           System.out.println("@AfterTest");  
      }  
      @BeforeSuite  
      public void beforeSuite() {  
           System.out.println("@BeforeSuite");  
           driver = new FirefoxDriver();  
      }  
      @AfterSuite  
      public void afterSuite() {  
           System.out.println("@AfterSuite");  
      }  

Selenium ScreenShots


Working with screen shots/ Print screen through selenium API

      public static String takeScreenShots(WebDriver driver) {  
           TakesScreenshot screen = (TakesScreenshot) driver;  
           File srcFile = screen.getScreenshotAs(OutputType.FILE);  
           copyFile(srcFile, "C:\\Test\\fileCopy");  
           driver.get("http://www.google.com");  
           String title = driver.getTitle();  
           return title;  
      }  
      public static void copyFile(File srcFile, String directoryPath) {  
           try {  
                // srcFile = new File("C:\\Test\\File1.txt");  
                File desDirectory = new File(directoryPath);  
                FileUtils.copyFileToDirectory(srcFile, desDirectory);  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  

Wednesday, January 6, 2016

FireFox ProfileManager

Windows

firefox.exe -ProfileManager \\ or firefox.exe -p


Mac

/Applications/Firefox.app/Contents/MacOS/firefox -profilemanager


Linux

./firefox -profilemanager

Selenium handling cookies



Retrieving cookie from website


 public class CookieExample {  
      static WebDriver driver;  
      static String webAddress = "http://xxxxxxxx:8080/xxxx/shared/config/config.jsp";  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           System.setProperty("webdriver.chrome.driver", "C:\\Java_Source_Code\\chromedriver_win32\\chromedriver.exe");  
           driver = new ChromeDriver();  
           Iterator<Cookie> cookies = login(driver, webAddress, null, "arsystem");  
           while (cookies.hasNext()) {  
 Cookie cookie = cookies.next();  
 System.out.println(cookie.getDomain());  
 System.out.println(cookie.getName());  
 System.out.println(cookie.getPath());  
 System.out.println(cookie.getExpiry());  
 System.out.println(cookie.getValue());  
 System.out.println(cookie.isSecure());  
           }  
           driver.close();  
      }  
      public static Iterator<Cookie> login(WebDriver driver, String webAddress, String loginID, String password) {  
           driver.get(webAddress);  
           driver.findElement(By.name("password")).clear();  
           driver.findElement(By.name("password")).sendKeys(password);  
           driver.findElement(By.className("button")).click();  
           new WebDriverWait(driver, 10)  
                     .until(ExpectedConditions.elementToBeClickable(driver.findElement(By.name("Logout"))));  
           driver.findElement(By.name("Logout")).click();  
           Set<Cookie> cookies = driver.manage().getCookies();  
           Iterator<Cookie> cookiesIterator = cookies.iterator();  
           return cookiesIterator;  
      }  
 }  

Selenium - FireFox Driver Usage

Use of FireFox Profile

Available constructors

Here we will see how Firefox driver can make use of a already created profile in firefox.
To check out which profile Firefox is using currently  enter about:support in Firefox address bar and check for "Profile Folder".

To create / modify/ delete a new Firefox profile 



To create, rename, or delete a profile, you have to perform the following steps:

1. Open the Firefox profile manager. To do that, in the command prompt

terminal, you have to navigate to the install directory of Firefox; typically, it

would in Program Files if you are on Windows. Navigate to the location where
you can find the firefox.exe file, and execute the following command:
firefox.exe -p
It will open the profile manager that will look like the following screenshot:
Note that before executing the above command, you need to make sure you
close all your currently running Firefox instances.
2. Use the Create Profile... button to create another profile, Rename Profile...
button to rename an existing profile, and Delete Profile... button to delete one.



Firefox Profile :
Firefox profile has a default constructor and other constructor taken an argument of type file.

Use of constructor FirefoxDriver(FirefoxProfile profile)

// using a already created profile

public static void fireFoxProfileA(WebDriver driver) { FirefoxProfile profile = new FirefoxProfile( new File("C:\\Users\\sudas\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\t1skcchd.sudas1")); driver = new FirefoxDriver(profile); driver.get("http://www.yahoo.com"); }

// Creating an instance of firefox profile for a specific plugin and store as JSON

public static void fireFoxProfileB(WebDriver driver) { FirefoxProfile profile = new FirefoxProfile(); try {  
// adding a extension to the new Firefox session
                profile.addExtension(new File("C:\\Java_Source_Code\\FireFoxExtensions\\firebug-2.0.13-fx.xpi"));  
                String json = profile.toJson();  
                // write the profile to a file  
                BufferedOutputStream bos = new BufferedOutputStream(  
                          new FileOutputStream(new File("C:\\Java_Source_Code\\FireFoxExtensions\\profile.json")));  
                bos.write(json.getBytes());  
   
                driver = new FirefoxDriver(profile);  
                driver.get("http://www.yahoo.com");  
                bos.flush();  
                bos.close();  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  



Use of preference with Firefox 

FirefoxDriver overwrites all the default preferences of Firefox in the user.js file
for you

List of preference
user_pref("extensions.update.notifyUser", false);
user_pref("security.warn_entering_secure.show_once", false);
user_pref("devtools.errorconsole.enabled", true);
user_pref("extensions.update.enabled", false);
user_pref("browser.dom.window.dump.enabled", true);
user_pref("offline-apps.allow_by_default", true);
user_pref("dom.disable_open_during_load", false);
user_pref("extensions.blocklist.enabled", false);
user_pref("browser.startup.page", 0);
user_pref("toolkit.telemetry.rejected", true);
user_pref("prompts.tab_modal.enabled", false);
user_pref("app.update.enabled", false);
user_pref("app.update.auto", false);
user_pref("toolkit.networkmanager.disable", true);
user_pref("browser.startup.homepage", "about:blank");
user_pref("network.manage-offline-status", false);
user_pref("browser.search.update", false);
user_pref("toolkit.telemetry.enabled", false);
user_pref("browser.link.open_newwindow", 2);
user_pref("browser.EULA.override", true);
user_pref("extensions.autoDisableScopes", 10);
user_pref("browser.EULA.3.accepted", true);
user_pref("security.warn_entering_weak", false);
user_pref("toolkit.telemetry.prompted", 2);
user_pref("browser.safebrowsing.enabled", false);
user_pref("security.warn_entering_secure", false);
user_pref("security.warn_leaving_secure.show_once", false);
user_pref("webdriver_accept_untrusted_certs", true);
user_pref("browser.download.manager.showWhenStarting", false);
user_pref("dom.max_script_run_time", 30);
user_pref("javascript.options.showInConsole", true);
user_pref("network.http.max-connections-per-server", 10);
user_pref("network.http.phishy-userpass-length", 255);
user_pref("extensions.logging.enabled", true);
user_pref("security.warn_leaving_secure", false);
user_pref("browser.offline", false);
user_pref("browser.link.open_external", 2);
user_pref("signon.rememberSignons", false);
user_pref("webdriver_enable_native_events", true);
user_pref("browser.tabs.warnOnClose", false);
user_pref("security.fileuri.origin_policy", 3);
user_pref("security.fileuri.strict_origin_policy", false);
user_pref("webdriver_assume_untrusted_issuer", true);
user_pref("startup.homepage_welcome_url", "");
user_pref("browser.shell.checkDefaultBrowser", false);
user_pref("browser.safebrowsing.malware.enabled", false);
user_pref("security.warn_submit_insecure", false);
user_pref("webdriver_firefox_port", 7055);
user_pref("dom.report_all_js_exceptions", true);
user_pref("security.warn_viewing_mixed", false);
user_pref("browser.sessionstore.resume_from_crash", false);
user_pref("browser.tabs.warnOnOpen", false);
user_pref("security.warn_viewing_mixed.show_once", false);
user_pref("security.warn_entering_weak.show_once", false);

To set the preference use the Key, Value pair
Use FireFox profile with preference

Firefox profile methods

Here I am using an already existing profile and setting a preference on the profile

      // using firefox profile along with preference   
      public static void fireFoxProfileA(WebDriver driver) {  
           FirefoxProfile profile = new FirefoxProfile(  
                     new File("C:\\Users\\sudas\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\t1skcchd.sudas1"));  
           //setting preference   
           profile.setPreference("browser.startup.homepage", "https://www.yahoo.co.in/");  
           driver = new FirefoxDriver(profile);  
 //          driver.get("http://www.yahoo.com");  
      }  

Monday, January 4, 2016

Selenium - WebDriver Wait




Implicit wait and Explicit wait

  public static void wait01(WebDriver driver) {  
           WebElement element = driver.findElement(By.name(""));  
           // implicit wait  
           driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
           // explicit wait  
           WebDriverWait explicitwait = new WebDriverWait(driver, 10);  
           explicitwait.until(ExpectedConditions.elementToBeClickable(element));  
      }  

Fluent Wait
      public static void wait02(WebDriver driver) {  
           // fluent wait  
           org.openqa.selenium.support.ui.Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)  
                     .withTimeout(10, TimeUnit.SECONDS)  
                     .pollingEvery(3, TimeUnit.SECONDS);  
           wait.until(new Function<WebDriver, WebElement>() {  
                @Override  
                public WebElement apply(WebDriver driver) {  
                     // TODO Auto-generated method stub  
                     return driver.findElement(By.name(""));  
                }  
           });  
      }