Sunday, September 24, 2017

Arrays in Java basics for selenium


1. We can assign value to an element in array as shown below:

testing[3] = 43; or all the elements can be defined in a go like shown below:
int[] testing1= {10,20,30,40,50,60,70,80,90,100};

2. We can extract all the values in an array using for each loop as shown below:

 int[] testing1 = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
 for (int test : testing1) {
     System.out.println(test);
 }

3. The same information can be extracted using for loop can be found as shown below:

for (int i = 0; i < testing1.length; i++) {
     System.out.println(testing1[i]);
 }

4. Using length property, we can find the size of any array


e.g. System.out.println(testing1.length)

4. We can pass an array as a parameter to a method, and also return value from a method:

5. We can sort an array in ascending order using sort();

Arrays.sort(testing1);

6. We can fill values in all the array elements using fill() menthod 
Arrays.fill(testing1, 385);

7. We can compare two arrays using arrays.equals

boolean isArrSame = Arrays.equals(testing1, testing);
System.out.println(isArrSame);

Sunday, July 23, 2017

Code to open firefox with selenium 3.4 using GeckoDriver 1.8


package test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class testing {

    public static void main(String[] args) throws InterruptedException {
 // Download the geckodriver based on the firefox if it is 32 bit or 64
 // bit installed in the machine
 // Save the driver and provide path of the exe for geckodriver
 System.setProperty("webdriver.gecko.driver", "E:\\Selenium\\geckodriver-v0.18.0-win64\\geckodriver.exe");
 // define the capabilities of firefox browser with marionette set as true
 DesiredCapabilities capabilities = DesiredCapabilities.firefox();
 capabilities.setCapability("marionette", true);
 // create an instance of firefox driver
 WebDriver driver = new FirefoxDriver(capabilities);
 driver.get("http://www.rediff.com");
 driver.quit();
    }
}

Saturday, July 22, 2017

Switch between Windows using switch to in Selenium


String MainWinHandle = driver.getWindowHandle();
for(String NewWinHandle : driver.getWindowHandles()){
// Ignore the parent window open and work with the new window frame
 if(!NewWinHandle.equals(MainWindowHandle))
  {
    driver.switchTo().window(NewWinHandle);
 //Perform the required tasks to be performed in the new window opened
 driver.close();
 }
}
//Perform action on the main window once the task is completed with the old window.
driver.switchTo().window(MainWinHandle);