硒与TestNG脚本不起作用?

时间:2019-07-04 18:23:23

标签: java selenium testng

带有TestNG脚本的硒不能正常工作吗?脚本在testng框架中不起作用,但在模块化框架中,它可以正常工作?这是代码快照。

//要在应用程序内部注册的脚本

public class AppTest{

        WebDriver driver;

        @BeforeMethod
        public void setup() {
            System.setProperty("webdriver.chrome.driver","F:\\ResourceFiles\\chromedriver_win32\\chromedriver_win32\\chromedriver.exe");
            WebDriver driver =new ChromeDriver();
            driver.get("http://newtours.demoaut.com/mercurywelcome.php");
            driver.manage().window().maximize();
    //      driver.manage().timeouts().implicitlyWait(8000, TimeUnit.SECONDS);
        }

        @Test
        public void registerApp(){

            //driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/a")).click();
            driver.findElement(By.linkText("REGISTER")).click();
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[2]/td[2]/input")).sendKeys("kartikey");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[3]/td[2]/input")).sendKeys("gautam");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[4]/td[2]/input")).sendKeys("7248006980");
            driver.findElement(By.xpath("//*[@id=\"userName\"]")).sendKeys("kartikeygautam@gmail.com");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[7]/td[2]/input")).sendKeys("22 Dayal Bagh Colony");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[9]/td[2]/input")).sendKeys("Agra");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[10]/td[2]/input")).sendKeys("UttarPradesh");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[11]/td[2]/input")).sendKeys("282005");

            Select fruits = new Select(driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[12]/td[2]/select")));
            fruits.selectByVisibleText("INDIA ");

            driver.findElement(By.xpath("//*[@id=\"email\"]")).sendKeys("kartikeygautam@gmail.com");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[15]/td[2]/input")).sendKeys("Mummyp@p@123");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[16]/td[2]/input")).sendKeys("Mummyp@p@123");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[16]/td[2]/input")).click();
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[18]/td/input")).click();
        }

        @Test
        public void loginApp() {
            driver.get("http://newtours.demoaut.com/mercurywelcome.php");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td[3]/form/table/tbody/tr[4]/td/table/tbody/tr[2]/td[2]/input")).sendKeys("kartikeygautam");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td[3]/form/table/tbody/tr[4]/td/table/tbody/tr[3]/td[2]/input")).sendKeys("Mummyp@p@123");
            driver.findElement(By.xpath("/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td[3]/form/table/tbody/tr[4]/td/table/tbody/tr[4]/td[2]/div/input")).click();  
        }
         @AfterMethod
         public void tearDown() {
             driver.quit();
         }  
    }

1 个答案:

答案 0 :(得分:0)

您的@BeforeMethod没有为驱动程序的类实例设置值,而是生成了一个新的驱动程序对象,该对象仅存在于@BeforeMethod的上下文中。您需要将@BeforeMethod调整为:

@BeforeMethod
public void setup() {
    System.setProperty("webdriver.chrome.driver","F:\\ResourceFiles\\chromedriver_win32\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get("http://newtours.demoaut.com/mercurywelcome.php");
    driver.manage().window().maximize();
}