I need to take snapshot of webpage and for that I am using selenium RC (this is good choice right ? )with eclipse for java language . I am using it as JUnit test case . Here is my code.
package com.example.tests;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
#SuppressWarnings("deprecation")
public class mainClassTest extends SeleneseTestCase {
#Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.gmail.com/");
//water= new DefaultSelenium("localhost", 4444, "*firefox", "http://www.gmail.com/");
selenium.start();
}
#Test
public void testFinalSelenium() throws Exception {
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("30000");
System.out.println("laoded\n");
// selenium.wait();
selenium.captureScreenshot("C:\\test\\simpleQuora.png");
selenium.captureEntirePageScreenshot("C:\\test\\CompleteQuora.png", "");
}
#After
public void tearDown() throws Exception {
selenium.stop();
}
}
And it's working fine but If i have to take snapshot of multiple URLs, then what are the ways to do that ?
Can we do it without using it as JUnit test case and using selenium in main function ?
Because if I try to run this code :
package com.example.tests;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class MainClass {
void func(String url, String file)
{
Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", url);
selenium.start();
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("30000");
System.out.println("laoded\n");
// selenium.wait();
String file1= "C:\\test\\"+file+".png";
String file2= "C:\\test\\"+file+"2.png";
selenium.captureScreenshot(file1);
selenium.captureEntirePageScreenshot(file2, "");
selenium.stop();
}
public static void main(String[] args)
{
MainClass demo = new MainClass();
demo.func("www.facebook.com","face");
//demo.func("www.reddit.com","reddit");
}
}
I got this error.(Although I have started server from cmd).
demo.func("www.facebook.com","face");
changes to
demo.func("http://www.facebook.com","face");
Related
I am fairly new to selenium, and I was trying to use some of the scripts being used in tutorials for my practice. I downloaded all the required .JAR files (Chrome drivers, Selenium Java and Stand Alone server) and added it to the path in Eclipse.
Below is the Code which I am trying to run to access a Weblink and then trying to verify if a user is able to log in successfully or not.
package learnautomation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class practice {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "Mypath\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.gcrit.com/build3/admin/");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Successful");
}
else {
System.out.println("Unsuccessful");
}
driver.close();
}
}
While doing this I am getting this error:
"Error occurred during initialization of boot layer
java.lang.module.FindException: Module seleniumnew not found"
Also, import org.openqa.selenium.chrome.ChromeDriver; it says this is not accessible as well when I just hover over it.
try this, just edit paths and package name.
package navi;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Avi {
public static WebDriver driver;
WebDriverWait wait5s = new WebDriverWait(driver,5);
#BeforeClass
public static void setUpClass() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\pburgr\\Desktop\\chromedriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\Users\\pburgr\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(options);
driver.manage().window().maximize();}
#Before
public void setUp() {}
#After
public void tearDown() {}
#AfterClass
public static void tearDownClass() {driver.close();driver.quit();}
#Test
public void avi() throws InterruptedException {
driver.get("http://www.gcrit.com/build3/admin/");
wait5s.until(ExpectedConditions.elementToBeClickable(By.name("username"))).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin#123");
driver.findElement(By.id("tdb1")).click();
// wait to resolve the click before checking url
Thread.sleep(1000);
String url = driver.getCurrentUrl();
if (url.equals("http://www.gcrit.com/build3/admin/index.php")){
System.out.println("Successful");
}
else {
System.out.println("Unsuccessful");
}
}
}
I am new to extent reporting. I am using Selenium Webdriver and want to use Extent reports with it.
But my code is not able to create ExtentReport object.
package com.code.draft;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class TestReport {
ExtentReports reports;
ExtentTest logger;
WebDriver driver;
public void start(){
reports = new ExtentReports("C:\\User\\Test\\Report\\Report.html"); //Exception at this line reports object = null
driver = new FirefoxDriver();
driver.get("http://www.google.com");
logger = reports.startTest("Verify Title");
logger.log(LogStatus.INFO, "Starting Browser");
reports.endTest(logger);
}
public static void main(String[] args) {
TestReport report = new TestReport();
report.start();
}
}
The above code is giving exception as :
Exception in thread "main" java.lang.NoSuchFieldError: VERSION_2_3_23
at com.relevantcodes.extentreports.HTMLReporter.start(HTMLReporter.java:76)
at com.relevantcodes.extentreports.Report.attach(Report.java:314)
at com.relevantcodes.extentreports.ExtentReports.<init>(ExtentReports.java:85)
at com.relevantcodes.extentreports.ExtentReports.<init>(ExtentReports.java:419)
at com.code.draft.TestReport.start(TestReport.java:19)
at com.code.draft.TestReport.main(TestReport.java:29)
Using the below configuration :
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
if anyone have idea. Please help.
I tested your code. It shows no exception at my end. But to get your HTML report you need to flush using reports.flush() just before reports.endTest(logger);.
I try to use a PhantomJS driver for my Selenium tests but I don't succeed to recover my Firefox profile to avoid me login in the website.
This is my code :
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Stackoverflow {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
System.setProperty("phantomjs.binary.path", System.getProperty("user.dir") + "/lib/phantomjs-2.1.1-windows/bin/phantomjs.exe");
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
driver = new PhantomJSDriver(capabilities);
driver.manage().window().maximize();
baseUrl = "http://stackoverflow.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testStackoverflow() throws Exception {
driver.get(baseUrl);
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
Can you tell me how to set PhantomJS driver ?
You are unable to use a firefox profile with PhantomJS because you're trying to use a firefox profile with PhantomJS... PhantomJS is not firefox, how did you think that was going to work.
Use this code to set the path of the phantomjs driver, Use this and let me know if you facing any issue:
File file = new File("E:\\software and tools\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
OR
System.setProperty("phantomjs.binary.path", "E:\\software and tools\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
WebDriver driver = new PhantomJSDriver();
I'm trying to write a test script that will login to facebook, but it falls short of clicking the login button. Where did I go wrong here?
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class GoogleRobotSearch {
private Selenium sel;
public GoogleRobotSearch () {
sel = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
sel.start();
}
public void search() {
sel.open("http://www.facebook.com");
sel.type("id=email","email");
sel.type("id=pass","password");
//sel.waitForPageToLoad("5000");
sel.click("//input[#value='Log In']");
}
public static void main (String args[]) {
GoogleRobotSearch xybot = new GoogleRobotSearch ();
xybot.search();
}
}
In webDriver we can do it like this
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.id("email")).sendKeys("mail");
driver.findElement(By.id("pass")).sendKeys("pwd");
driver.findElement(By.id("loginbutton")).click();
}
For chrome driver:
public static void main(String[] args)
{
WebDriver driver= new ChromeDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.id("email")).sendKeys("mail");
driver.findElement(By.id("pass")).sendKeys("pwd");
driver.findElement(By.id("loginbutton")).click();
}
First make sure you have chrome driver with environment variable path. If you don't have, go to http://code.google.com/p/chromedriver/downloads/list. Download and set path. Use this for set path
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
I am new to the world of testing. I am using selenium IDE for recording the tests. Also, I am exporting the test case as JUnit4 test case. My export looks like:
package com.example.tests;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Test {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://192.168.8.207/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void test() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.name("user")).clear();
driver.findElement(By.name("user")).sendKeys("admin");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("infineta123");
driver.findElement(By.id("btnLogin-button")).click();
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
How to execute this test case? Now after that, how can I automate the execution of several such test cases?
Use this code as runner for your tests.
import org.junit.runner.JUnitCore;
import com.example.tests;
public static void main(String[] args) {
Result result = JUnitCore.runClasses(Test.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
Test.class is the file name(Test) containing the code for tests. You can add List of Classes if you have multiple test cases.
Look at this page: http://code.google.com/p/selenium/wiki/UsingWebDriver
You will probably have to download the selenium and import it to the project.
If this is done, you can go to the "Test" - it depends on developement tool you are using - In NetBeans I do it via Run - Test File (Ctrl + F6 I think)