-3

I'm currently taking the Selenium tutorial Here and I've followed every step exactly, but my Eclipse program keeps throwing errors. I am using Selenium 3 while this tutorial is for an older version. I can't find any comprehensive tutorials aside from this one. How do I fix the errors in the following code? I've commented the exact errors I get after each line. The code already contained some comments, so ignore any comment at the start of a line. Everything else should be error messages.

I also need to know how to use Eclipse to set my classpath, to allow it access to the GeckoDriver, which may or may not fix the issues.

public class Gmail_Login { //Syntax error on token(s), misplaced construct(s)
import org.openqa.selenium.By; //The import org.openqa.selenium.By cannot be resolved
import org.openqa.selenium.WebDriver; // The import org.openqa.selenium.WebDriver cannot be resolved
import org.openqa.selenium.WebElement;// The import org.openqa.selenium.WebElement cannot be resolved
import org.openqa.selenium.firefox.FirefoxDriver;// The import org.openqa.selenium.firefox. cannot be resolved

    /**

    * @param args

    */

           public static void main(String[] args) { //Multiple markers at this line -Syntax error,insert "enum Identifier" to complete EnumHeader   -Syntax error on tokens, AnnotationName expected instead   -Syntax error on token "}",invalid (    -Syntax error, insert")" to complete SingleMemberAnnotation    -Syntax error, insert "]" to complete ArrayAccess



    // objects and variables instantiation

                  WebDriver driver = new FirefoxDriver();//Multiple markers at this line   -FirefoxDriver cannot be resolved to a type   -WebDriver cannot be resolved to a type

                  String appUrl = "https://accounts.google.com";



    // launch the firefox browser and open the application url

                  driver.get(appUrl);



    // maximize the browser window

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



    // declare and initialize the variable to store the expected title of the webpage.

                  String expectedTitle = " Sign in - Google Accounts ";



    // fetch the title of the web page and save it into a string variable

                  String actualTitle = driver.getTitle();



    // compare the expected title of the page with the actual title of the page and print the result

                  if (expectedTitle.equals(actualTitle))

                  {

                         System.out.println("Verification Successful - The correct title is displayed on the web page.");

                  }

                 else

                  {

                         System.out.println("Verification Failed - An incorrect title is displayed on the web page.");

                  }


    // enter a valid username in the email textbox

                  WebElement username = driver.findElement(By.id("Email"));//Multiple markers at this line   -WebElement cannot be resolved to a type

                  username.clear();

                  username.sendKeys("TestSelenium");


    // enter a valid password in the password textbox

                  WebElement password = driver.findElement(By.id("Passwd")); //Multiple markers at this line    -WebElement cannot be resolved to a type   -By cannot be resolved    -By cannot be resolved    


                  password.clear(); 
                  password.sendKeys("password123");



    // click on the Sign in button

                  WebElement SignInButton = driver.findElement(By.id("signIn")); //Multiple markers at this line   -WebElement cannot be resolved to a type     -By cannot be resolved 

                  SignInButton.click();


    // close the web browser

                  driver.close();

                  System.out.println("Test script executed successfully.");


    // terminate the program

                  System.exit(0);
           }






}//Syntax error on token "}", delete this token
Matt Brennan
  • 133
  • 1
  • 1
  • 11
  • 1
    Don't imports go outside of the class? Like in the tutorial you're following? Also, since you're targeting a different version of Selenium, you probably won't be able to randomly assume everything is the same. – Dave Newton Oct 18 '16 at 16:22
  • Selenium 3 is very new. (was released 5 days ago). It's safe to bet that thorough tutorials haven't been written for Selenium 3 yet :) – ddavison Oct 18 '16 at 16:24
  • @DaveNewton even when i put them outside the class, all of the same errors return. I was under the impression that i'd want to define/import them outside the class, as to make it easier to reference them later with scope. I'm not sure if Selenium even has that ability though. – Matt Brennan Oct 18 '16 at 16:26
  • 2
    @MattBrennan Selenium has what ability? Selenium is a library; you have basic Groovy/Java compilation errors to deal with first. – Dave Newton Oct 18 '16 at 16:28
  • @DaveNewton Was refering to accessing objects from somewhere else, I remember some languages could do that, possibly like java or JS. And yeah java would handle that i suppose. – Matt Brennan Oct 18 '16 at 16:30
  • *I also need to know how to use Eclipse to set my classpath* -- If you haven't yet done that, then *that* is the issue (along with the placement of the import statements) – OneCricketeer Oct 18 '16 at 16:31
  • @cricket_007 I'm guessing that would be something i'd learn in Java? Could you leave a link to a tutorial or explain how I might do that? – Matt Brennan Oct 18 '16 at 16:34
  • @MattBrennan Any Eclipse tutorial; the process is the same for Groovy or Java (roughly). – Dave Newton Oct 18 '16 at 16:35
  • You need to put the classes that are not built-in to Java on the classpath in order for them to resolve, yes. [Setting up Eclipse for Selenium](http://toolsqa.com/selenium-webdriver/configure-eclipse-with-selenium-webdriver/) – OneCricketeer Oct 18 '16 at 16:35

2 Answers2

0

I think all I did was move the imports outside of the class and deleted one } and the errors went away. I fixed the indents and removed all the error related comments. Try this...

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

public class Gmail_Login
{
    public static void main(String[] args)
    {
        // objects and variables instantiation
        WebDriver driver = new FirefoxDriver();
        String appUrl = "https://accounts.google.com";

        // launch the firefox browser and open the application url
        driver.get(appUrl);

        // maximize the browser window
        driver.manage().window().maximize();

        // declare and initialize the variable to store the expected title of the webpage.
        String expectedTitle = " Sign in - Google Accounts ";

        // fetch the title of the web page and save it into a string variable
        String actualTitle = driver.getTitle();

        // compare the expected title of the page with the actual title of the page and print the result
        if (expectedTitle.equals(actualTitle))
        {
            System.out.println("Verification Successful - The correct title is displayed on the web page.");
        }
        else
        {
            System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
        }

        // enter a valid username in the email textbox
        WebElement username = driver.findElement(By.id("Email"));
        username.clear();
        username.sendKeys("TestSelenium");

        // enter a valid password in the password textbox
        WebElement password = driver.findElement(By.id("Passwd"));
        password.clear();
        password.sendKeys("password123");

        // click on the Sign in button
        WebElement SignInButton = driver.findElement(By.id("signIn"));
        SignInButton.click();

        // close the web browser
        driver.close();
        System.out.println("Test script executed successfully.");
        // terminate the program
        System.exit(0);
    }
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • I copy/pasted this into my client and renamed the package and class respectively. It throws the error for .sendKeys() saying the arguement is not applicable for strings. – Matt Brennan Oct 18 '16 at 18:03
  • Looking at [this page](http://stackoverflow.com/questions/23485363/the-method-sendkeyscharsequence-in-the-type-webelement-is-not-applicable-for) it sounds like you may be using a really old version of Java? I would make sure you have the most recent JRE and version of Selenium installed and any other library you might be using. – JeffC Oct 18 '16 at 18:07
  • My libraries were up to date, It turns out I just needed to set my compiler compliance to 1.7. – Matt Brennan Oct 18 '16 at 18:14
0

I figure everything I did was move the imports outside of the class and erased one } and the blunders disappeared. I fixed the indents and eliminated all the mistake related remarks. Attempt the following coding:

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

public class Gmail_Login
{
    public static void main(String[] args)
    {
        // objects and variables instantiation
        WebDriver driver = new FirefoxDriver();
        String appUrl = "https://accounts.google.com";

        // launch the firefox browser and open the application url
        driver.get(appUrl);

        // maximize the browser window
        driver.manage().window().maximize();

        // declare and initialize the variable to store the expected title of the webpage.
        String expectedTitle = " Sign in - Google Accounts ";

        // fetch the title of the web page and save it into a string variable
        String actualTitle = driver.getTitle();

        // compare the expected title of the page with the actual title of the page and print the result
        if (expectedTitle.equals(actualTitle))
        {
            System.out.println("Verification Successful - The correct title is displayed on the web page.");
        }
        else
        {
            System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
        }

        // enter a valid username in the email textbox
        WebElement username = driver.findElement(By.id("Email"));
        username.clear();
        username.sendKeys("TestSelenium");

        // enter a valid password in the password textbox
        WebElement password = driver.findElement(By.id("Passwd"));
        password.clear();
        password.sendKeys("password123");

        // click on the Sign in button
        WebElement SignInButton = driver.findElement(By.id("signIn"));
        SignInButton.click();

        // close the web browser
        driver.close();
        System.out.println("Test script executed successfully.");
        // terminate the program
        System.exit(0);
    }
}