Thursday, 29 May 2014

Multiple window handling using selenium webdriver.

package RandD;

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

 public class testing {

public static void main(String[] args ) throws InterruptedException {
WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("URL");
               
                driver.findElement(By.id("")),click();

                // opening in new window

 Set<String> windowOpened = driver.getWindowHandles();
System.out.println("No of opened windows  "+windowOpened.size());
Iterator<String> it = windowOpened.iterator();
String Windows1[] = new String[windowOpened.size()];
int i = 0;
System.out.println("===========================");
while(it.hasNext()){
Windows1[i] =  it.next();
System.out.println(Windows1[i]);
i++ ;
}

driver.switchTo().window(Windows1[1]);
//do some operation in new window
driver.close();
driver.switchTo().window(Windows1[0]);
                        // do operation in main window
                         driver.close();
            }
      }

Highlighting the web element using selenium webdriver.

package RandD;

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

public class testing {

public static void main(String[] args ) throws InterruptedException {
WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://www.facebook.com/");
WebElement element= driver.findElement(By.id("email"));
element.sendKeys("ngowda");
//for (int i = 0; i < 2; i++) {

       JavascriptExecutor js = (JavascriptExecutor) driver;
       js.executeScript("arguments[0].setAttribute('style', arguments[1]);",

               element, "color: red; border: 2px solid red;");
}
}

Monday, 26 May 2014

Drag and drop a file or element from Source to Target place using selenium.

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Vanmeter {



public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.get("url"); // enter ur url here
driver.findElement(By.xpath("")).click(); //Enter the xpath locator in brace and click on the element
Actions act = new Actions(driver);
WebElement srcElement = driver.findElement(By.id(""));// enter the source element xpath or id
Thread.sleep(3000);
WebElement targetElement = driver.findElement(By.id(""));//enter the target xpath or id.
Thread.sleep(3000);
act.dragAndDrop(srcElement, targetElement);
Thread.sleep(3000);
act.build().perform();
Thread.sleep(3000);

}

}

Finding the broken links in the website using selenium.

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import Framework.Driver;


public class Brokenimages {
public static int invalidLink;
    String currentLink;
    String temp;

public static void main(String[] args) throws IOException {
WebDriver driver = new FirefoxDriver();
driver.get("Url");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

List<WebElement> ele=driver.findElements(By.tagName("a"));
        System.out.println("size:" +ele.size() );
        for(int i=0;i<ele.size();i++) {
            System.out.println(ele.get(i).getAttribute("href"));
            int statusCode=0;
            try{
            statusCode=getResponseCode(ele.get(i).getAttribute("href"));
           // ele.get(i).click();
          // driver.navigate().back();
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            if(statusCode==404) {
            System.err.println("InvalidLink :"+ele.get(i).getAttribute("href"));
                File scrFile4 = ((TakesScreenshot)Driver.driver).getScreenshotAs(OutputType.FILE);
   
    FileUtils.copyFile(scrFile4, new File("D:\\tmp\\image_"+new Date().getTime()+" .png"));
            }else {
            System.out.println("ValidLinks "+ele.get(i).getAttribute("href"));
}

        }

}

private static int getResponseCode(String attribute) throws IOException {
URL u = new URL(attribute);
       HttpURLConnection h =  (HttpURLConnection)  u.openConnection();
       h.setRequestMethod("GET");
       h.connect();
       return h.getResponseCode();

}
}

You can changed the Anchor tags and checking the all links like "img" and "src".

Selenium web driver Save the downloaded file to particular given path.



import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.remote.DesiredCapabilities;



public class download {


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

String profilePath = "TestNG";
FirefoxProfile profile = new ProfilesIni().getProfile(profilePath);
String path="E:\\Download";
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", path);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword,application/csv,text/csv,image/png ,image/jpeg, application/pdf, text/html,text/plain,application/octet-stream");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.closeWhenDone", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);

profile.setPreference("pdfjs.disabled", true);

WebDriver driver = new FirefoxDriver(profile);

driver.get("http://docs.seleniumhq.org/download/");
 driver.manage().window().maximize();
driver.findElement(By.xpath("//a[@href='http://selenium-release.storage.googleapis.com/2.41/selenium-java-2.41.0.zip']")).click();
 System.out.println("Success");