Friday 17 July 2015

How to handle Ajax popup window?

By using getWindowHandles() and obj.switchTo.window(windowid) 
we can handle popups using 
explicit wait and driver.swtchT0.window("name") commands for your requirements.

How to refresh a page ?

1.Using sendKeys.Keys method
2.Using navigate.refresh() method
3.Using navigate.refresh() method
4.Using get() method
5.Using sendKeys() method
1.Using sendKeys.Keys method
driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);
2.Using navigate.refresh() method
driver.get("http://ruchi-myseleniumblog.blogspot.in/2013/12/100-selenium-interview-questions.html");
driver.navigate().refresh();
3.Using navigate.to() method
driver.get("http://ruchi-myseleniumblog.blogspot.in/2014/01/selenium-hybrid-framework-using.html");
driver.navigate().to(driver.getCurrentUrl());
4.Using get() method
driver.get("http://ruchi-myseleniumblog.blogspot.in/2013/12/basic-core-java-interview-questions.html");
driver.get(driver.getCurrentUrl());
5.Using sendKeys() method
driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");

How to scroll web element

FirefoxProfile profile=new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver=new FirefoxDriver(profile);
driver.navigate("http://jqueryui.com/draggable/");
Thread.sleep(6000L);
WebElement element=driver.findElement(By.xpath("//div[@id='draggable']"));
Actions actn=new Actions(driver);
actn.dragAndDropBy(element, 50, 50).build().perform();

select an item from a dropdown list using Selenium WebDriver

List<WebElement> options = select.findElements(By.tagName("option"));

for (WebElement option : options) {

if("Germany".equals(option.getText().trim()))

 option.click();   
}

Webdriver SELECT Methods to work with Dropdowns

<html> <head> <title>Select Example by Index value</title> </head> <body> <select name="Mobiles"><option value="0" selected> Please select</option> <option value="1">iPhone</option> <option value="2">Nokia</option> <option value="3">Samsung</option> <option value="4">HTC</option> <option value="5">BlackBerry</option> </select> </body> </html>


--------------------------
select.selectByValue(Value);
WebElement element=driver.findElement(By.name("Mobiles")); Select se=new Select(element); se.selectByIndex(1);  


------------------------------------WebElement element=driver.findElement(By.name("Mobiles")); Select se=new Select(element); se.selectByVisibleText("HTC");   
-------------------------
<html> <head> <title>Select Example by Value</title> </head> <body> <p>Which mobile device do you like most?</p> <select name="Mobiles"><option selectd> Please select</option> <option value="iphone">iPhone</option> <option value="nokia">Nokia</option> <option value="samsung">Samsung</option> <option value="htc">HTC</option> <option value="blackberry">BlackBerry</option> </select> </body> </html>
Webdriver code for Selecting a Value using select.selectByValue(Value);
public class selectExamples { WebDriver driver; @Test public void selectSamples() { driver = new FirefoxDriver(); driver.get("C:\\Users\\DEV\\Desktop\\html-and-css-code-samples\\Final Code\\chapter-07\\dropdown-select.html"); WebElement element=driver.findElement(By.name("Mobiles")); Select se=new Select(element); se.selectByValue("nokia"); } }
 deselect methods.
WebElement element=driver.findElement(By.name("Mobdevices")); Select se=new Select(element); //Here we will take multi select dropdown to show you the difference se.selectByVisibleText("HTC"); se.selectByValue("nokia"); //From the above two commands, in the dropdown two values will be selected. //Now we will try to deselect any of the One //Im using thread to see the difference when selecting and selecting Thread.sleep(3000); se.deselectByValue("nokia"); //You can deselect the value by specifying the index, value and VisibleText //It will work if you the index is already selected se.deselectByIndex(1); //It will deselect if the visible text HTC is in selected mode se.deselectByVisibleText("HTC"); //It will de-select all the values which are selected se.deselectAll();

Work with keyboard in selenium

driver.findElement(By.xpath("xpath")).sendKeys(Keys.chord(Keys.CONTROL, "a"));

Type casting in selenium webdriver

    public By idReturn(String id){
        id = id.toLowerCase();
        if(id.startsWith("id~")) return By.id(id.replace("id~", ""));
        else if(id.startsWith("name~")) return By.name(id.replace("name~", ""));
        else if(id.startsWith("cssselector~")) return By.cssSelector(id.replace("cssselector~", ""));
        else if(id.startsWith("xpath~")) return By.xpath(id.replace("xpath~", ""));
       
        return By.xpath("Unable to find the element locater");
    }
   
    public boolean enterText(String id, String value){
        if(value.length() == 0) return false;
        driver.findElement(idReturn(id)).clear();
        driver.findElement(idReturn(id)).sendKeys(value);
        return true;
    }
    public void clickElement(String id){
        driver.findElement(idReturn(id)).click();
    }

Implicit and explicit wait

driver.manage().timeouts().implicitlyWait(A_GIVEN_NUMBER, TimeUnit.SECONDS);


WebDriverWait wait = new WebDriverWait(driver, 40);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

Double click on element using Selenium webdriver


Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]"))).doubleClick().perform();

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>fms_webapp_testing</groupId>
  <artifactId>fms_webapp_testing</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
 
 
  <build>
    <sourceDirectory>src</sourceDirectory>
    <testSourceDirectory>src</testSourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.15</version>
          <configuration>
              <includes>
                 <include>**/SuiteRunner.java</include>
            </includes>
           
      </configuration>   
      </plugin>
    </plugins>
  </build>
  <dependencies>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
      </dependency>
  <dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.12</version>
  </dependency>
        

  <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.10.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.10-FINAL</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.10.1</version>
</dependency>

<dependency>
    <groupId>javax.xml.stream</groupId>
    <artifactId>stax-api</artifactId>
    <version>1.0-2</version>
</dependency>

<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.4.0</version>
</dependency>

   
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.35.0</version>
    </dependency>
   
      <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>2.44.0</version>
   
    </dependency>
          <dependency>
        <groupId>net.sourceforge.jexcelapi</groupId>
        <artifactId>jxl</artifactId>
        <version>2.6</version>
    </dependency>
     
  </dependencies>
</project>