2017-07-18 37 views
0

我試圖讓POM(PageFactory)基於TestNG的框架,但我面臨的問題與傳遞webdriver的實例,第二TestCase的文件..如何使用TestNG.XML一個接一個地運行多個PageFactory頁面?

這是我的試驗基地(初始化瀏覽器和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; 

    } 

上面的代碼我在我的測試PAGE..ie 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 protected]"); 
    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的......但在這裏是問題..我的NE xt測試案例..如果我通過相同的行即WebDriver驅動程序= 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" 
+0

解決。使用單例類 - https://www.tutorialspoint.com/java/java_using_singleton.htm引用此頁欲瞭解更多詳情 –

+0

不知道辛格爾頓如何在這裏幫助。 – bbk

+0

當你說driver.get(url)時,讓webDriver對象方法爲singleton.so,它會加載新的url而不是啓動新的驅動程序 –

回答

1

如何創建@BeforeCl屁股並做你的驅動程序初始化那裏 喜歡的東西:

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

這將只執行一次,並會做初始化。

如果你只擔心執行順序,你將不得不做出改變2:1,在你的testng.xml文件中,你將不得不補充:

測試名稱=「測試」 保存-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完成之前啓動。如果需要保證,那麼聲明一個依賴關係。

與聲明依賴關係的解決方案不同,即使一個測試失敗,也會執行使用優先級的測試。依賴關係的這個問題可以用@Test(... alwaysRun = true ...)根據http://testng.org/doc/documentation-main.html#annotations

+0

非常感謝@anshulGupta爲你的時間和解決方案工作 – bbk

相關問題