Selenium webdriver using Java - with Eclipse, is not working

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Selenium webdriver using Java - with Eclipse, is not working

Abhay
I am trying to use Selenium Webdriver with Java. I am using Eclipse IDE to implement. Steps performed by me -
1. Created a Java Project
2. Added "selenium-java-2.17.0" in [JavaBuildPath->Library]
3. Added "junit-4.9" in [JavaBuildPath->Library]
4. Wrote my code

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

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

driver.get("http://www.google.co.in");

}
}
Expected Output - This code should launch Firefox & open google site.

Actual Output - throws error
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Reply | Threaded
Open this post in threaded view
|

Re: Selenium webdriver using Java - with Eclipse, is not working

softwaretestingforum
Administrator
Hello Abhay,

Welcome to the forum.

Which version of java are you using. Type "java -v" from command line and you will know

Since you are just beginning with Selenium I suggest to use TestNG the Junit for your tests.
You can download TestNG jar from here - http://testng.org/testng-6.3.1.zip
unzip it and add it to eclipse. Exactly how you did for Junit.

Copy following code in your class -

protected WebDriver driver;
       
        @Test
        public void testWithLocator ()
        {
                driver = new FirefoxDriver();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                driver.get("http://www.google.com/");
                driver.findElement(By.id("lst-ib")).sendKeys("Selenium Rocks");
                driver.quit();
        }


Now you need to use Eclipse TestNG plugin to execute the test from IDE.
Follow the instruction on this page - http://testng.org/doc/eclipse.html and execute test.

Let me know how it goes.

~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Selenium webdriver using Java - with Eclipse, is not working

Illusion0Reality
With Junit or TestNG, we don't need to write Public static void main() at all. That is the main benefit for these.

I simply write functions like below:


//All pages related function calls will be placed here. This is is the Driver/Main page.
@Test
public void allPages()   throws Exception
{
for (av.pagerows =0; av.pagerows<=pnamearr.length-1 ;av.pagerows++)
{
String pval =pnamearr[av.pagerows];
if ( pval.contains(TestEndflag) | av.exitTest==true )
{ av.exitTest=false;
//driver.close();
break;
}
pval=pnamearr[pagerows];
AllKeywords keyword = AllKeywords.valueOf(pval);
switch (keyword)
{
case login:
Login logob= new Login();
logob.LoginPage(keyword.toString());
break;