我试图在 Selenium Web Driver 中实现继承。在 Countrychoser 类中,我从 Baseurl 类中调用了 Basic() 方法。当我尝试在 TestNG 中执行时,浏览器调用了两次。但是在 TestNG.xml 中,我只提到了 Countrychoser 类。
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 {
public static WebDriver driver;
@Test
public static void basic()
{
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.get("http://www.sears.com/shc/s/CountryChooserView?storeId=10153&catalogId=12605");
}
public static void Closebrowser()
{
driver.quit();
}
}
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 extends Baseurl
{
@Test
public static void Choser()
{
try
{
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();
}
}
}
测试NG.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="First" >
<classes>
<class name="MyTestNG.Countrychoser" />
</classes>
</test>
</suite>