Basic Selenium Test

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

 

//Automating Facebook


public class TestProject {

public static void main(String[] args) throws InterruptedException {
 

// TODO Auto-generated method stub

  By webButton = By.name("websubmit");
  WebDriver d = new FirefoxDriver();

 

//Launch Browser
  d.get("https://www.facebook.com/");

  //Input box first name and lastname 
  d.findElement(By.id("u_0_1")).sendKeys("Bharath");
  d.findElement(By.id("u_0_3")).sendKeys("Sannadi");

 

//Input box
 d.findElement(By.name("reg_email__")).sendKeys("8504642312");
 d.findElement(By.name("reg_email_confirmation__")).sendKeys("8504642312");
 d.findElement(By.name("reg_passwd__")).sendKeys("ABCabc12");

 

//Drop Downs 
  d.findElement(By.name("birthday_month")).sendKeys("Jul");
  d.findElement(By.name("birthday_day")).sendKeys("18");
  d.findElement(By.name("birthday_year")).sendKeys("1984");

 

//radio Button
  d.findElement(By.id("u_0_e")).click();
  Thread.sleep(3000);

 

//Web Button
d.findElement(webButton).click();

}

}

Launching Different Browsers

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;


public class MainClass {
    
    static WebDriver driver = null ; 
    static String Browsertype = "dd";

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        
        if (Browsertype.equals("InternetExplorer") ){
            DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
            dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
            System.setProperty("webdriver.ie.driver", "C:\\Users\\Sagari\\Desktop\\Selenium-AutomationDependency\\IEDriverServer.exe");
            driver = new InternetExplorerDriver(dc);
            driver.get("https://www.google.com/?gws_rd=ssl");
        }
        else if(Browsertype.equals("chrome")){
            DesiredCapabilities dc = DesiredCapabilities.chrome();
            //dc.setCapability(ChromedriverOptio)
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\Sagari\\Desktop\\Selenium-AutomationDependency\\chromedriver.exe");
             driver = new ChromeDriver(dc);
             driver.get("https://www.google.com/?gws_rd=ssl");   
        }
        else{
             driver = new FirefoxDriver();
        }     
    }

}

Find Elements

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;


public class MainClass {
    
    static WebDriver driver = null ; 

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
         driver = new FirefoxDriver();
         driver.get("https://www.facebook.com/");
         List<WebElement> WebElements = driver.findElements(By.tagName("a"));
         System.out.println(WebElements.size());
         for(WebElement WebEle:WebElements){
             if(WebEle.getText().equals("Terms")){
                 WebEle.click();
                 break;
             }
         }
        
    }

}

Browser Operations

public class MainClass {
    
    static WebDriver driver = null ; 

    public static void main(String[] args) throws InterruptedException {
        
        driver = new FirefoxDriver();
        driver.get("https://www.dice.com/");
        driver.findElement(By.linkText("Advanced Job Search")).click();
        driver.navigate().back();
        Thread.sleep(2000);
        driver.navigate().forward();
        Thread.sleep(2000);
        driver.navigate().refresh();
        Thread.sleep(2000);
    }

}

Getting CSS properties, attributes

public class MainClass {
    
    static WebDriver driver = null ; 

    public static void main(String[] args) throws InterruptedException {
        
        driver = new FirefoxDriver();
        driver.get("https://www.facebook.com/");
        String fontSize = driver.findElement(By.id("u_0_1")).getCssValue("font-size");
        System.out.println(fontSize);
        
        String width = driver.findElement(By.id("u_0_1")).getCssValue("width");
        System.out.println(width);
        
        String name = driver.findElement(By.id("u_0_1")).getAttribute("name");
        System.out.println(name);
        
        
        String ariarequired = driver.findElement(By.id("u_0_1")).getAttribute("aria-required");
        System.out.println(ariarequired);    


    }

}

Firefox Profile

public class MainClass {
    
    static WebDriver driver = null ; 

    public static void main(String[] args) {
        
    
         FirefoxProfile fp = new FirefoxProfile();
         fp.setAcceptUntrustedCertificates(true);
         fp.setEnableNativeEvents(true);
        
        //Launch of Browser and navigate to dice.com
         driver = new FirefoxDriver(fp);
         driver.get("http://www.dice.com");
         
         
    }

}

Multi Select

public class MainClass {
    
    static WebDriver driver = null ; 

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //Launch of Browser and navigate to dice.com
         driver = new FirefoxDriver();
         driver.get("http://www.dice.com");
         
         //Click on advanced Search
         driver.findElement(By.linkText("Advanced Job Search")).click();
         
         //Synchronization  for the next page and then it will return WebElement 
         WebDriverWait wait = new WebDriverWait(driver,10);
         WebElement WebEle = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("searchTerms")));
         WebEle.sendKeys("Testing");
         
         WebElement WebElementStateList = driver.findElement(By.id("STATE_LIST"));
         
         //Select class is used for multi select
         Select selectStateList = new Select(WebElementStateList);
         selectStateList.selectByVisibleText("Arkansas");
         selectStateList.selectByValue("CA");
         selectStateList.selectByIndex(8);
         
         selectStateList.deselectAll();
         
    }

}

Print Print | Sitemap
© 2016 Automation Learn. All rights reserved.