Home > Blockchain >  problem when write test code for selenium using java, some methods is undefined?
problem when write test code for selenium using java, some methods is undefined?

Time:06-23

I am newbie with Java and Selenium and here is my problem.

I wrote a very simple test code by java and I added selenium webdriver java 3.141 as the tutorial and have no problem.

My problem is, my code has some bugs as you can see in this picture'

Here is my code

package seleniumFirstProject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
//comment the above line and uncomment below line to use Chrome
//import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumFirstTest {
public static void main(String[] args) {
    // declaration and instantiation of objects/variables
    System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
    ChromeDriver driver = new ChromeDriver();
    //comment the above 2 lines and uncomment below 2 lines to use Chrome
    //System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
    //WebDriver driver = new ChromeDriver();
    
    String baseUrl = "http://demo.guru99.com/test/newtours/";
    String expectedTitle = "Welcome: Mercury Tours";
    String actualTitle = "";

    // launch Chrome and direct it to the Base URL
    driver.get(baseUrl);

    // get the actual value of the title
    actualTitle = driver.getTitle();

    /*
     * compare the actual title of the page with the expected one and print
     * the result as "Passed" or "Failed"
     */
    if (actualTitle.contentEquals(expectedTitle)){
        System.out.println("Test Passed!");
    } else {
        System.out.println("Test Failed");
    }
    //close Chrome
    driver.close();
   }
}

Here is the picture

picture about my problem

Here is my error :

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method get(String) is undefined for the type ChromeDriver
The method getTitle() is undefined for the type ChromeDriver
The method close() is undefined for the type ChromeDriver

at seleniumFirstProject/seleniumFirstProject.SeleniumFirstTest.main(SeleniumFirstTest.java:22)

As you can see at the bottom of the picture, there are 3 messages about my errors. All of them are the method xxx is undefined ...

So how can I fix them ?

Sorry because I am newbie with java and selenium. Thank you for your help.

CodePudding user response:

I think that I could occur due to you didn't load maven project and you have the dependencies. So you can create a maven project How do you create maven project and later you must put inside pom.xml those dependencies

<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-java</artifactId>
   <version>4.1.2</version>
</dependency>
<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-chrome-driver</artifactId>
   <version>4.0.0</version>
</dependency>

More information about maven

CodePudding user response:

In above error lies here in these 2 lines.

System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
ChromeDriver driver = new ChromeDriver();

You are setting the property of geckodriver[i.e. Firefox Driver], and below you are calling ChromeDriver Class.

You can do this by putting the below code in replace of that.

System.setProperty("webdriver.chrome.driver", # path of chromedriver #);
WebDriver driver = new ChromeDriver();

So the explanation goes this way. Here you're setting the property of chromedriver in 1st statement. And in the last statement, you have to use the WebDriver driver instead of the ChromeDriver driver.

The line says that it is creating a ChromeDriver instance and storing it in a variable called driver which is of type "WebDriver" interface. Now all your methods as per above example get(), getTitle(), close() are implemented in WebDriver. Therefore, you cannot use methods of WebDriver Interface while creating an object of ChromeDriver class. By doing this we can achieve runtime polymorphism by running the same script on a different browser.

I hope this satisfies your question.

  • Related