2017-07-18 6 views
0

これは私のテストベース(ブラウザとのlog4jを初期化する)TestNG.XMLを使用して複数のPageFactoryページを1つずつ実行する方法

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; 

    } 
..です

を私はPOM(PageFactory)ベースのTestNGフレームワークを作るしようとしていますが、私は第二のTestCaseファイルにインスタンスwebdriverを渡すと問題に直面しています私は(下)私のテストPAGE..ie InputFormSubmitPageに

私のTestCaseすなわちInputFormSubmitPageTestで今
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*****************"); 

    } 

を延長しています上記のコード

私はwebdriverをを初期化することができるよ...しかし、ここで問題..です私の中ネ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つの変更を加える必要があります、実行順序についてだけ心配している:あなたのtestng.xmlファイル1.を、あなたが追加する必要があります:

テスト名=「テスト」保存します-order = "true" これにより、testng.xmlファイルに記述されているテストクラスの実行順序が保証されます。優先順位は、実行順序を奨励していますが、以前の優先順位を保証するものではありません

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

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

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

:あなたはクラス内での優先順位を確認したい場合には、より詳細http://www.seleniumeasy.com/testng-tutorials/preserve-order-in-testng

ため、このリンクをたどって、あなたは次のように何かをする必要がありますレベルが完了しました。 test2が完了する前にtest3を開始できます。保証が必要な場合は、依存関係を宣言します。

依存関係を宣言するソリューションと異なり、優先度を使用するテストは、1つのテストが失敗しても実行されます。依存関係のこの問題は、@ Test(... alwaysRun = true ...)を使用して回避することができます。http://testng.org/doc/documentation-main.html#annotations

+0

あなたの時間と解決のために@anshulGuptaに感謝します – bbk

関連する問題