Pages

Tuesday 20 November 2012

Running local script in remote machine

How to run the local script in remote machine?
In manual testing we often used to do cross browser testing. The same thing we can do by automation using selenium webdriver. Followings are some steps to run  a local script in a remote machine.

Steps:
1. Download "selenium-server-standalone-2.*.jar" in the remote machine. Download from here
(If this jar is deprecated please try for a new jar.Just type selenium standalone jar for web driver in Google for new jar)
Note: Download the jar file in remote machine not in the local machine.

2. Open the command prompt in the remote machine and run the following command "java -jar < downloaded jar filepath>". This command will run a selenium server. If it will not able to run the selenium server, please just open this link in browser and click on  "okk".
Note: Be sure that java has been installed in the client machine. If not installed please install java first.

3. Then run the following java code from the machine from which you are going to initiate the remote machine.
import java.io.File;
import java.net.URL;
import org.openqa.selenium.OutputType;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
public class RemotelyRunningScript {
public static void main(String s[]) throws Exception {
System.out.println("coming to this only");
URL url = new URL( "http", "local host", 4444, "/wd/hub" );
DesiredCapabilities capabilities =DesiredCapabilities.firefox();
System.out.println("1");
capabilities.setJavascriptEnabled(true);
System.out.println("2");
WebDriver driver = new RemoteWebDriver(url,capabilities);
System.out.println("4");
driver.get("http://www.yahoo.com");
//File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
//FileUtils.copyFile(scrFile, new File("c:\\screenshot\\googlesearch-webdriverapi.png"));
System.out.println("opened yahoo page succesfully");
driver.quit();
}
}

Note: In place of "local host" please provide the remote IP. e.g. 192.145.12.45



Monday 19 November 2012

Take Screenshot using Selenium Webdriver

Why Screenshot is required ?
While doing manual testing we often need to take the screenshot in case we come up with any issue. We can do the same thing for the Automation testing as well. While running a script there might be a chance of fail of the script. So in that case we have to take the screen shot for that failed script in order to report as an issue.
I am giving a simple example how to take the screenshot after opening "google.com" in firefox browser :-

     1. WebDriver driver = new FirefoxDriver(); // Opening the Firefox browser       
      2. driver.get("http://www.google.com/");
      3. File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); // This
          will take the screen shot and will store in srcFile file.
      4. FileUtils.copyFile(scrFile, new File("File Path Location")); // Here you can change the
           path location you want.

Note : File path should contain the file name at the end. e.g. : c:\\temp\\screenshot.png.

After executing the code :
Just go to the path specified and you will able to find a screenshot.