Question

  1. Getting the error "driver cannot be resolved" for running iam commenting out
  2. Unable to run the Testng suite.

How to fix the issue.

Baseurl.java

package MyTestNG;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Baseurl
{

@Test
public static WebDriver basic()
{
WebDriver driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.get("http://www.sears.com/shc/s/CountryChooserView?storeId=10153&catalogId=12605");
return driver;
}

public static void Closebrowser()
{
   driver.quit();   ///  Iam getting this error, "**driver cannot be resolved**"  
                                     have done mistake i don't know
}
}

Countrychoser.java

package MyTestNG;
import org.testng.annotations.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.thoughtworks.selenium.Selenium;
import org.openqa.selenium.support.ui.Select;
import java.util.*;
import java.io.*;

public class Countrychoser
{

    public static void Choser()
    {
    try
    {
    WebDriver driver = Baseurl.basic();
    //driver.findElement(By.className("box_countryChooser")).click();
    driver.findElement(By.id("intselect")).sendKeys("India");
    driver.findElement(By.xpath(".//*[@id='countryChooser']/a/img")).click();
    //window.onbeforeunload = null;
    System.out.println("---------------------------------------");
    System.out.println("Country choser layer test case-Success");
    System.out.println("---------------------------------------");
        }

    catch(Exception e)
    {
          // Screenshot.pageScreenshot();
           System.out.println(e);
            System.out.println("---------------------------------------");
            System.out.println("Country choser layer test case Failed");
            System.out.println("---------------------------------------");
          }
    finally {
          // Screenshot.pageScreenshot(); 
          // Baseurl.Closebrowser();
          }
    }
}

and the XML Suite, below

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1" allow-return-values="true">
  <test name="First" >
    <classes>
       <class name="MyTestNG.Countrychoser" />
    </classes>
  </test> 
  </suite>

When iam running the TestNg suite getting the below error.

Method public static org.openqa.selenium.WebDriver MyTestNG.Baseurl.basic() has a @Test annotation but also a return value: ignoring it. Use <suite allow-return-values="true"> to fix this
[TestNG] Running:
  C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--1777632869\testng-customsuite.xml


===============================================
    Default test
    Tests run: 0, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@13ad88b: 16 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@e0c07c: 31 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@9b1ac: 0 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@182d86: 110 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1548414: 93 ms

** can some one please help me for correcting the issue. **

Was it helpful?

Solution

In Baseurl.java, driver is not declared as a global variable. It is declared under basic(). This means driver is accessible only under basic().closeBrowser() cannot have access to driver, hence you are getting the error.

I have a few suggestions to your code.

  1. I believe Baseurl.java is not the class under test. It is provided merely to start and close the browser. So remove the annotation @Test from it.

  2. Countrychoser.java is your test class. So add @Test to it before the method Choser(). I also suggest you make the Choser() method non static.

  3. I see that you have used finally block to close the browser. Since you are using the TestNG framework, you can explore the possibilities of @AfterTest annotation instead.

  4. Similarly @BeforeTest annotation is also useful.

To sum it up, please have at a look at your code with the modifications :-

Baseurl.java

public class Baseurl {
    private WebDriver driver; //global variable

    public static WebDriver basic() {
        driver = new FirefoxDriver();
        driver.manage().deleteAllCookies();
        driver.get("http://www.sears.com/shc/s/CountryChooserView?storeId=10153&catalogId=12605");
       return driver;
    }

    public static void Closebrowser(){
        driver.quit();   //  You wont get an error now :)
    } 
}

Countrychoser.java

public class Countrychoser {
    WebDriver driver;

    @BeforeTest
    public void startTest(){
        driver = Baseurl.basic();
    } 

    @Test
    public void Choser() {
        try {
            //driver.findElement(By.className("box_countryChooser")).click();
            driver.findElement(By.id("intselect")).sendKeys("India");
            driver.findElement(By.xpath(".//*[@id='countryChooser']/a/img")).click();
            //window.onbeforeunload = null;
            System.out.println("---------------------------------------");
            System.out.println("Country choser layer test case-Success");
            System.out.println("---------------------------------------");
        } catch(Exception e) {
            // Screenshot.pageScreenshot();
            System.out.println(e);
            System.out.println("---------------------------------------");
            System.out.println("Country choser layer test case Failed");
            System.out.println("---------------------------------------");
        }
    }

    @AfterTest
    public void endTest() {  
        // Screenshot.pageScreenshot(); 
        // Baseurl.Closebrowser();
    }
}

Let me know if this helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top