方法public static org.openqa.selenium.WebDriver MyTestNG.Baseurl.basic()错误

时间:2014-02-07 17:45:05

标签: java selenium-webdriver testng

  1. 运行iam评论
  2. 时出现错误“驱动程序无法解析”
  3. 无法运行Testng套件。
  4. 如何解决问题。

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

    和下面的XML Suite

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

    **有人可以帮我纠正这个问题。 **

1 个答案:

答案 0 :(得分:2)

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

我对您的代码提出了一些建议。

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

  2. Countrychoser.java是您的测试类。所以在方法Choser()之前添加@Test。我还建议你使Choser()方法不是静态的。

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

  4. 同样@BeforeTest注释也很有用。

  5. 总结一下,请查看您的代码并进行修改: -

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

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