Java 方法public static org.openqa.selenium.WebDriver MyTestNG.Baseurl.basic()错误 获取运行iam注释时出现的错误“驱动程序无法解决” 无法运行Testng套件

Java 方法public static org.openqa.selenium.WebDriver MyTestNG.Baseurl.basic()错误 获取运行iam注释时出现的错误“驱动程序无法解决” 无法运行Testng套件,java,selenium-webdriver,testng,Java,Selenium Webdriver,Testng,如何解决这个问题 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;

如何解决这个问题

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
}
}
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

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();
          }
    }
}
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();
    }
}
以及下面的XML套件

<!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>

当iam运行TestNg套件时,出现以下错误

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
Method public static org.openqa.selenium.WebDriver MyTestNG.Baseurl.basic()有一个@Test注释,但也有一个返回值:忽略它。用于修复此问题
[TestNG]正在运行:
C:\Users\Administrator\AppData\Local\Temp\testngeclipse--1777632869\testng-customsuite.xml
===============================================
默认测试
测试运行:0,失败:0,跳过:0
===============================================
===============================================
默认套件
运行的测试总数:0,失败:0,跳过:0
===============================================
[TestNG]org.TestNG.reporters花费的时间。EmailableReporter2@13ad88b:16毫秒
[TestNG]org.TestNG.reporters花费的时间。XMLReporter@e0c07c:31毫秒
[TestNG]通过[FailedReporter=0失败=0跳过=0]所用的时间:0毫秒
[TestNG]org.TestNG.reporters花费的时间。JUnitReportReporter@9b1ac:0毫秒
[TestNG]org.TestNG.reporters花费的时间。SuiteHTMLReporter@182d86:110毫秒
[TestNG]org.TestNG.reporters.jq花费的时间。Main@1548414:93毫秒

**谁能帮我纠正一下这个问题吗**

在Baseurl.java中,驱动程序未声明为全局变量。它在basic()下声明。这意味着只能在basic()下访问驱动程序。closeBrowser()无法访问驱动程序,因此您将收到错误

我对你的代码有一些建议

  • 我相信Baseurl.java不是测试中的类。它仅用于启动和关闭浏览器。因此,从中删除注释@Test

  • java是您的测试类。因此,在方法Choser()之前添加@Test。我还建议您将Choser()方法设置为非静态

  • 我看到您已使用finally块关闭浏览器。由于您使用的是TestNG框架,因此可以探索@AfterTest注释的可能性

  • 类似地,@BeforeTest注释也很有用

  • 总而言之,请查看您的代码及其修改:-

    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
    }
    }
    
    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

    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();
              }
        }
    }
    
    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();
        }
    }
    

    如果这对您有帮助,请告诉我。

    我很惊讶,请帮助我。非常感谢您的解释,您的解决方案对我很有效。