40074

Method public static org.openqa.selenium.WebDriver MyTestNG.Baseurl.basic() Error

Question:

<ol><li>Getting the error "driver cannot be resolved" for running iam commenting out</li> <li>Unable to run the Testng suite.</li> </ol>

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. **

Answer1:

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.

<ol><li>

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.

</li> <li>

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.

</li> <li>

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.

</li> <li>

Similarly @BeforeTest annotation is also useful.

</li> </ol>

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.

Recommend

  • How can I click on a button using Selenium WebDriver with Java?
  • Selenium Parallel testing with extension methods
  • Remove xml tag during xstream parsing?
  • Is there a way to read an Excel file using Dataflow
  • Send http request through specific network interface
  • How could I write a BsonDocument object into a file, and read it again, using Java
  • Java PrintWriter output looks weird in notepad
  • When I try to upload and save an image file using Struts in eclipse it shows the file name as null
  • PHP websocket on SSL with proxy_wsTunnel - Apache
  • Extracting a process's exit code in the case of ThreadInterrupted
  • C# Remove URL from String
  • Google datalab : how to import pickle
  • css font-size and line-height not matching the baseline
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Yii2: Using Kartik Depdrop Widget?
  • Graceful pod termination
  • insert a picture into database(sqlite) with java code. what should i do?
  • getelement by class name for clicking
  • Getting error java.io.FileNotFoundException (log4j log file) at the time of publish project on cloud
  • Is looping through all style sheets and classes a good idea in JavaScript?
  • Android Database Error - getWriteableDatabase
  • Using Generics on right hand side in Java 6?
  • What is the purpose of TaskExecutor in spring?
  • how to find common suffix in java by using method
  • Hardware Accelerated Image Scaling in windows using C++
  • Change JButton Shape while respecting Look And Feel
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Cross-Platform Protobuf Serialization
  • Can I make an Android app that runs a web view in Chrome 39?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • ORA-29908: missing primary invocation for ancillary operator
  • jQuery tmpl and DataLink beta
  • How to get next/previous record number?
  • How to format a variable of double type
  • How to include full .NET prerequisite for Wix Burn installer
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • How does Linux kernel interrupt the application?
  • JaxB to read class hierarchy
  • Unable to use reactive element in my shiny app