Tuesday, 29 December 2015

Working with multiple browsers or Multiple windows

package selenium;

import java.util.Set;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class MultipleBrowser  {
    WebDriver driver;
  @Test
  public void multipleBrowsers() throws InterruptedException {
           driver=new FirefoxDriver();
     
          driver.get("https://www.google.co.in/");
          System.out.println(driver.getTitle());
         
//Get a list window handles which are currently opened (the list will contain more than one handle if there are any child windows (popups) opened from parent window, otherwise it will contain only 1 set)
          Set<String> listofallfirstbrowserrelatedwindows = driver.getWindowHandles();
          System.out.println(listofallfirstbrowserrelatedwindows);

//Get the Parent window handle for the firstbrowser window and assgine to firstbrowser
          String firstbrowser = driver.getWindowHandle();
          System.out.println(driver.getTitle());

//open a new window using "executeScript"JavaScript but under same driver umbrella (Means same driver will be able to communicate with this browser also as it is opend under same driver instance)
          ((JavascriptExecutor)driver).executeScript("window.open();");

//Get latest list of the currently open windows (this is supposed to return all the browser handles which are openend under same driver instance (in our case it will be 2))
          Set<String> listofallsecondbrowserrelatedwindows = driver.getWindowHandles();
          System.out.println(listofallsecondbrowserrelatedwindows);
         
// Remove all the browser handle which were openend prior to above second window (in this case, only first browser handle will be removed and second browser handle will remain)   
        listofallsecondbrowserrelatedwindows.removeAll(listofallfirstbrowserrelatedwindows);         
          System.out.println(listofallsecondbrowserrelatedwindows);         

// Assign the above second window handle to a string     
          String secondbrowser = ((String)listofallsecondbrowserrelatedwindows.toArray()[0]);
          System.out.println(secondbrowser);

// NOTE : NOW YOU HAVE TWO SEPERATE HANDLE IN TWO STRINGS (firstbrowser, secondbrowser), WHICH YOU CAN USE with drive to switch between both browsers.

// switch to secondbrowser (i.e. second window)         
          driver.switchTo().window(secondbrowser);
          driver.get("https://in.yahoo.com/");
        Thread.sleep(2000);     

// switch to firstbrowser (i.e. first window)         
          driver.switchTo().window(firstbrowser);
          driver.manage().window().maximize();
        Thread.sleep(2000);
// switch to secondbrowser (i.e. second window)         
          driver.switchTo().window(secondbrowser);
         
  }

}

Working on Multiple Frames

package abcpack;

import static org.junit.Assert.assertEquals;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MultiFrame {
    WebDriver d;
    @Test
    public void testMultiFrame() throws Exception
    {
        //Load the web page
        d.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
        assertEquals("Overview",d.getTitle());
        List<WebElement> f=d.findElements(By.tagName("frame"));
        System.out.println("No of frames:"+f.size());
        //Switch driver focus to 1st frame
        d.switchTo().frame(0);
        d.findElement(By.linkText("com.thoughtworks.selenium")).click();
        d.switchTo().defaultContent();
        //Switch driver focus to 2nd frame
        d.switchTo().frame(1);
        d.findElement(By.linkText("Selenium")).click();
        d.switchTo().defaultContent();
        //Switch driver focus to 3rd frame
        d.switchTo().frame(2);
        assertEquals("Interface Selenium",d.findElement(By.className("title")).getText());
       
        Thread.sleep(4000);
    }
    @Before
    public void setUp()
    {
        //Launch browser
        d=new FirefoxDriver();
         d.manage().window().maximize();
        d.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
    @After
    public void tearDown()
    {
        //Close browser
        d.quit();
    }


}

Remove duplicates from Array in java

package java_interview;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Duplicate_String_Remover_Without_String
{
    static int[]  arr={1,2,3,1,4,5,3};
   
public static void main(String[] args) {
   
    //Way 1 pass
    /*TreeSet<String> set=new TreeSet<String>();
    set.add("pabbu");
    set.add("reddy");
    set.add("java_practice");
    set.add("reddy");
    set.add("aaa");
    System.out.println("printing all values from tree set: " + set);
    Iterator<String> itr=set.iterator();
        while(itr.hasNext())
        {
        System.out.println("printin tree values after iterating: " + itr.next());       
   
        }
*/
   
    //Way 2 pass   
   
        int end = arr.length;
        Set<Integer> set = new HashSet<Integer>();
        for (int i = 0; i < end; i++) {
            set.add(arr[i]);
        }
        Iterator it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }


}

Write code for Palindrome and reverce string. e.g madam, dad




Way 1: Using For loop  :
package java_interview;

public class Polindrom_Using_ForLoop {
    static String original="abac";
  
    public static void main(String[] args) {
      
  
        String reverse="";
        int length = original.length();
        
          for ( int i = length - 1 ; i >= 0 ; i-- )
             reverse = reverse + original.charAt(i);
    
          if (original.equals(reverse))
             System.out.println(original+" is a palindrome.");
          else
             System.out.println(original+" is not a palindrome.");
    }
}


Way2 : Using String Builder:

package java_interview;

public class Polindrom_Test {
static String txt="madam";
public static void main(String[] args)
{
//Using string builder
String strbuilderTxt=new StringBuilder(txt).reverse().toString();
System.out.println("Printing string using string builder class : " + strbuilderTxt);
if(txt.equals(strbuilderTxt))
{
    System.out.println("Ploindrome :");
}
else {
    System.out.println("Not a Ploindrome :");
}
}
}
 

Selenium Grid



 Selenium-Grid allows you run your tests on different machines against different browsers in parallel.
Start hubjava -jar selenium-server-standalone-2.38.0.jar -role hub
Start nodejava -jar selenium-server-standalone-2.38.0.jar -role node  -hub http://localhost:4444/grid/register
Create RemoteWebDriver object
WebDriver driver = new RemoteWebDriver(hub-url,capabilities);

Difference between JUnit&TestNG?




Selenium code for 'google search' auto suggestions and print result?



public class Auto_Suggestions {  
   
WebDriver driver;  
   
 @BeforeTest  
 public void start(){  
   driver = new FirefoxDriver();   
 }  
    
 @Test  
  public void SearchSuggestion() {  
    
  driver.get("http://google.com");  
  driver.findElement(By.id("gbqfq")).sendKeys("v2tech");  
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
    
   WebElement table = driver.findElement(By.className("gssb_m"));   
   List<WebElement> rows = table.findElements(By.tagName("tr"));   
   Iterator<WebElement> i = rows.iterator();   
   System.out.println("-----google auto suggestions are:---------");   
   while(i.hasNext()) {   
           WebElement row = i.next();   
           List<WebElement> columns = row.findElements(By.tagName("td"));   
           Iterator<WebElement> j = columns.iterator();   
           while(j.hasNext()) {   
                   WebElement column = j.next();   
                   System.out.println(column.getText());   
           }   
           System.out.println("");   
              
   System.out.println("google auto suggestions are:");   
   }   
  }   
}