如果我在所有测试 类 中创建一个 Web Driver Instance 是否可以,或者是否有任何方法可以创建它一次并在测试项目中随处使用
Is it ok if I create a Web Driver Instance in all my test classes or is there any way around to create it once and use everywhere in the test project
我通常在整个项目的所有测试 类 中创建一个 Web 驱动程序实例,然后使用 Firefox 驱动程序对其进行初始化。这是正常的自动化实践还是我应该只创建一次并在我的项目中到处使用它。如果第二个是 'should be' 情况,那么如何实现它。
我在一个项目中使用网络驱动程序 Java,在另一个项目中使用 C#。任何帮助将不胜感激。
您可以在超级class 中创建一个 webdriver 实例,并在需要时在子classes(测试classes)中使用它。因为这将节省每个 class.
初始化 webdriver 的时间
您可以在 Base 类中声明您的驱动程序,如下所示:
public class BaseClass {
static WebDriver driver;
@BeforeSuite
public void setup() throws InterruptedException, IOException {
driver = new FirefoxDriver();
driver.manage().window().maximize();
Properties obj = new Properties();
FileInputStream objfile = new FileInputStream(
System.getProperty("user.dir")
+ "\src\com\provider\Object.Properties");
obj.load(objfile);
driver.get(obj.getProperty("URL"));
}
}
然后你可以像下面那样扩展你的基础 class 这样你就不需要在你创建的每个 class 中声明你的驱动程序:
public class ProApp extends BaseClass{
@Test(priority=1)
public void clickLoginLink() throws InterruptedException, IOException {
Properties obj = new Properties();
FileInputStream objfile = new FileInputStream(System.getProperty("user.dir") +"\src\com\provider\Object.Properties");
obj.load(objfile);
driver.findElement(By.xpath(obj.getProperty("ClickOnLoginLink"))).click();
Thread.sleep(1000);
}
}
我通常在整个项目的所有测试 类 中创建一个 Web 驱动程序实例,然后使用 Firefox 驱动程序对其进行初始化。这是正常的自动化实践还是我应该只创建一次并在我的项目中到处使用它。如果第二个是 'should be' 情况,那么如何实现它。
我在一个项目中使用网络驱动程序 Java,在另一个项目中使用 C#。任何帮助将不胜感激。
您可以在超级class 中创建一个 webdriver 实例,并在需要时在子classes(测试classes)中使用它。因为这将节省每个 class.
初始化 webdriver 的时间您可以在 Base 类中声明您的驱动程序,如下所示:
public class BaseClass {
static WebDriver driver;
@BeforeSuite
public void setup() throws InterruptedException, IOException {
driver = new FirefoxDriver();
driver.manage().window().maximize();
Properties obj = new Properties();
FileInputStream objfile = new FileInputStream(
System.getProperty("user.dir")
+ "\src\com\provider\Object.Properties");
obj.load(objfile);
driver.get(obj.getProperty("URL"));
}
}
然后你可以像下面那样扩展你的基础 class 这样你就不需要在你创建的每个 class 中声明你的驱动程序:
public class ProApp extends BaseClass{
@Test(priority=1)
public void clickLoginLink() throws InterruptedException, IOException {
Properties obj = new Properties();
FileInputStream objfile = new FileInputStream(System.getProperty("user.dir") +"\src\com\provider\Object.Properties");
obj.load(objfile);
driver.findElement(By.xpath(obj.getProperty("ClickOnLoginLink"))).click();
Thread.sleep(1000);
}
}