gpt4 book ai didi

java - 如何使用 TestNG.XML 一对一地运行多个 PageFactory 页面?

转载 作者:行者123 更新时间:2023-12-02 12:35:48 26 4
gpt4 key购买 nike

我正在尝试制作基于 POM (PageFactory) 的 TestNG 框架,但我面临着将 WebDriver 实例传递到第二个 TestCase 文件的问题..

这是我的测试库(用于初始化浏览器和 log4j)

public class TestBase {
public static WebDriver driver;
public static FileInputStream fip;
public static Properties prop;
//public static Logger APP_LOGS=null;
//public static SoftAssert st=null;
public static boolean TestFail=false;
public static int temp=0;
public static final Logger APP_LOGS=Logger.getLogger(TestBase.class.getName());

public static WebDriver initialization() throws Throwable{
fip=new FileInputStream("./Files/or.properties");
prop=new Properties();
prop.load(fip);
//APP_LOGS.debug("properties file is loaded");
String browser=prop.getProperty("browsertype");
//System.out.println("5");
if(browser.equalsIgnoreCase("mozilla")){
System.setProperty("webdriver.gecko.driver", "./drivers/geckodriver.exe");
driver= new FirefoxDriver();
//APP_LOGS.debug("Mozilla fire fox browser started");
}
else if (browser.equalsIgnoreCase("ie")){
System.setProperty("webdriver.ie.driver", "./drivers/IEDriverServer.exe");
driver=new InternetExplorerDriver();
//APP_LOGS.debug("InternetExplorer browser started");
} else if(browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
driver=new ChromeDriver();
//APP_LOGS.debug("Chrome browser started");
}
driver.get(prop.getProperty("url"));
//driver.manage().window().maximize();
String log4jConfPath = "log4j.properties";
PropertyConfigurator.configure(log4jConfPath);
APP_LOGS.info("Opened "+prop.getProperty("browsertype")+" browser");
APP_LOGS.info("Navigated to Seleniumeasy.com/test");

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
return driver;

}

上面的代码是我在测试页面中扩展的。即InputFormSubmitPage(如下)

public class InputFormSubmitPage extends TestBase{

WebDriver driver;

public InputFormSubmitPage(WebDriver driver) {
this.driver=driver;
PageFactory.initElements(driver, this);
}

//INPUTFORM SUBMIT -- Objects Locators

@FindBy(how=How.XPATH, using="//*[@id='treemenu']/li/ul/li[1]/a") WebElement inputformlink;
@FindBy(how=How.XPATH, using="//*[@id='treemenu']/li/ul/li[1]/ul/li[5]/a") WebElement inputFormSubmit;
@FindBy(how=How.CSS, using="[name='first_name'][placeholder='First Name']") WebElement firstName;
@FindBy(how=How.CSS, using="[name='last_name'][placeholder='Last Name']") WebElement lastName;
@FindBy(how=How.CSS, using="[name='email'][placeholder='E-Mail Address']") WebElement eMail;
@FindBy(how=How.CSS, using="[name='phone'][data-bv-field='phone']") WebElement phoneNumber;
@FindBy(how=How.CSS, using="[name='address'][placeholder='Address']") WebElement address;
@FindBy(how=How.CSS, using="[name='city'][placeholder='city']") WebElement city;
@FindBy(how=How.CSS, using=".form-control.selectpicker") WebElement state;
@FindBy(how=How.CSS, using="[name='zip'].form-control") WebElement zipCode;
@FindBy(how=How.CSS, using="[name='comment'][placeholder='Project Description']") WebElement projDescription;
@FindBy(how=How.CSS, using=".btn.btn-default") WebElement sendButton;
//@FindBy(how=How.CSS, using="div[class$='has-error']>div>small[data-bv-result='INVALID']") WebElement allFieldsValidationErrorMessages_Invalid;
//@FindBy(how=How.CSS, using="div[class$='has-error']>div>small[data-bv-result='VALID']") WebElement allFieldsValidationErrorMessages_Valid;

@FindBy(css="div[class$='has-error']>div>small[data-bv-result='INVALID']") public List<WebElement> allFieldsValidationErrorMessages_Invalid;

public void enterInputFormDetails()
{
inputformlink.click();
inputFormSubmit.click();
firstName.sendKeys("FirstName");
lastName.sendKeys("LastName");
eMail.sendKeys("eMail@email.com");
phoneNumber.sendKeys("9008001242");
address.sendKeys("1234, 1st street");
city.sendKeys("City");

//State Selector
Select oneState= new Select(state);
oneState.selectByIndex(3);

zipCode.sendKeys("12345");
projDescription.sendKeys("This is Project Description");
sendButton.click();

APP_LOGS.info("*****************InputFormSubmit Button is clicked*****************");

}

现在在我的测试用例中,即InputFormSubmitPageTest,我能够初始化WebDriver...但这就是问题..在我的下一个测试用例中..如果我通过同一行,即WebDriver driver=TestBase.initialization();浏览器是再次初始化..我只是想避免这种情况..但不确定如何执行我正在使用TestNG.xml来运行顺序执行

public class InputFormSubmitPageTest { //My First TestCase

@Test
public void validatingFieldsData() throws Throwable
{
WebDriver driver=TestBase.initialization(); // this is where i am starting browser

InputFormSubmitPage formSubmit=PageFactory.initElements(driver, InputFormSubmitPage.class);
formSubmit.inputFormLaunch();
formSubmit.inputFormSubmitInValidValidations();
formSubmit.enterInputFormDetails();
}

}

我的第二个测试用例,即 AjaxFormSubmitPageTest

public class AjaxFormSubmitPageTest { //My Send TestCase

@Test
public static void validatingFieldsData() throws Throwable
{
WebDriver driver=TestBase.initialization(); // this is where i am starting browser

AjaxFormSubmitPage formSubmit=PageFactory.initElements(driver, AjaxFormSubmitPage.class);
formSubmit.inputFormLaunch();
formSubmit.inputFormSubmitInValidValidations();
formSubmit.enterInputFormDetails();
}
}

我的 TestNG.xml 包含以下条目...

class name="testcases.InputFormSubmitPageTest"
class name="testcase.AjaxFormSubmitPageTest"

最佳答案

如何创建一个 @BeforeClass 并在那里进行驱动程序初始化。像这样的东西:

@BeforeClass
public static void before() {
WebDriver driver=TestBase.initialization();
}

这只会执行一次并进行初始化。

如果您只担心执行顺序,则必须进行 2 项更改: 1. 在 testng.xml 文件中,您必须添加:

测试名称=“测试”preserve-order=“true”这将确保testng.xml文件中提到的测试类的执行顺序。请点击此链接了解更多详细信息 http://www.seleniumeasy.com/testng-tutorials/preserve-order-in-testng

如果您想确保类中的优先顺序,您将必须执行以下操作:

@Test(priority=1) public void Test1() {}

@Test(priority=2) public void Test2() {}

@Test(priority=3) public void Test3() {}

优先级鼓励执行顺序,但不保证先前的优先级已经完成。 test3 可以在 test2 完成之前开始。如果需要保证,则声明依赖关系。

与声明依赖项的解决方案不同,即使一个测试失败,使用优先级的测试也会执行。这个依赖关系问题可以根据 http://testng.org/doc/documentation-main.html#annotations 使用 @Test(...alwaysRun = true...) 来解决

关于java - 如何使用 TestNG.XML 一对一地运行多个 PageFactory 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45163235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com