Running WebDriver Test In Headless Browser Using PhantomJS GhostDriver

Earlier nosotros bring configured PhantomJS GhostDriver amongst eclipse In previous postal service to run Selenium WebDriver attempt In headless browser. Now nosotros are all ready to execute sample WebDriver attempt In eclipse using PhantomJS GhostDriver. I bring created sample attempt equally bellow which volition move executed In headless browser.

We volition use DesiredCapabilities to set phantomjs.exe executable file's path as well as and hence nosotros volition operate past times It amongst PhantomJSDriver Initialization equally shown In bellow example.

Note : Please ready proper path of phantomjs.exe file In bellow given examples.

package Testing_Pack;  import java.io.IOException; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.phantomjs.PhantomJSDriverService; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  world shape phantomjs {  WebDriver driver;    @BeforeTest  world void setup() throws Exception {   //Set phantomjs.exe executable file path using DesiredCapabilities.   DesiredCapabilities capability = novel DesiredCapabilities();     capability.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "E:/phantomjs-2.0.0-windows/bin/phantomjs.exe");   driver = novel PhantomJSDriver(capability);   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);    }    @Test  world void phantomTest() throws IOException{   driver.get(" ");   //Get electrical flow page championship using javascript executor.     JavascriptExecutor javascript = (JavascriptExecutor) driver;     String pagetitle=(String)javascript.executeScript("return document.title");       System.out.println("My Page Title Is  : "+pagetitle);       driver.findElement(By.xpath("//input[@id='2']")).click();   driver.findElement(By.xpath("//input[@id='plus']")).click();   driver.findElement(By.xpath("//input[@id='3']")).click();   driver.findElement(By.xpath("//input[@id='equals']")).click();   String total = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");   System.out.println("****** Sum Is : "+sum+" ******");    } }

When yous run to a higher house illustration In eclipse, It volition execute attempt In headless PhantomJS GhostDriver as well as impress page championship as well as total effect In console equally bellow.


Capturing page screenshot
Question : Is It possible to capture page screenshot If your attempt Is executed In headless browser using PhantomJS? Answer Is Yes. You tin expire page screenshot when attempt Is beingness executed In headless browser.

Bellow given illustration volition capture page screenshot as well as salve file amongst cite "Test.jpeg" In D: drive.
package Testing_Pack;  import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit;  import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.phantomjs.PhantomJSDriverService; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  world shape phantomjs {  WebDriver driver;    @BeforeTest  world void setup() throws Exception {   //Set phantomjs.exe executable file path using DesiredCapabilities.   DesiredCapabilities capability = novel DesiredCapabilities();     capability.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "E:/phantomjs-2.0.0-windows/bin/phantomjs.exe");   driver = novel PhantomJSDriver(capability);   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);    }    @Test  world void phantomTest() throws IOException{   driver.get(" ");   //Get electrical flow page championship using javascript executor.     JavascriptExecutor javascript = (JavascriptExecutor) driver;     String pagetitle=(String)javascript.executeScript("return document.title");       System.out.println("My Page Title Is  : "+pagetitle);       driver.findElement(By.xpath("//input[@id='2']")).click();   driver.findElement(By.xpath("//input[@id='plus']")).click();   driver.findElement(By.xpath("//input[@id='3']")).click();   driver.findElement(By.xpath("//input[@id='equals']")).click();   String total = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");   System.out.println("****** Sum Is : "+sum+" ******");      //To capture page screenshot as well as salve In D: drive.   File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);   FileUtils.copyFile(scrFile, novel File("D:\\Test.jpeg"),true);  } }

Execute to a higher house attempt as well as opened upward D: travail when execution completed. There volition move "Test.jpeg" In D: drive. This way, You tin capture page screenshot for phantomjs driver attempt case.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post