如何停止呼叫登录两次

时间:2016-02-23 10:38:23

标签: java selenium testng

我已经创建了一个login.java文件用于登录。并在DematAccount中使用了参数。 java文件登录。我的问题是当我运行demat.xml时..登录执行两次。第一件衣服运行而不是再次登录,而第二件衣服运行。

Login.java

     public class Login 
     {

        public static FirefoxDriver driver = new FirefoxDriver();

         @Test(priority = 0)
         @Parameters({"username","password"})
          public void login(String username, String password)
          {
             driver.get("here is my url");
             driver.findElement(By.id("UserName")).sendKeys(username);
             driver.findElement(By.id("Password")).sendKeys(password);
             driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
             driver.findElement(By.id("btnLogin")).sendKeys(Keys.RETURN);
         }
      } 

DematAccount.java

       public class DematAccount {

         WebDriver driver;
         Login lgn = new Login();


       @Test(priority = 0)
       @Parameters({ "username", "password" })
        public void DematAccount1(String username, String password) {
        driver = Login.driver;
        lgn.login(username, password);
       }

         @Test(priority = 1)
          public void open_dematAccount() {
          driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
           driver.findElement(By.xpath("(//div[@class='item-content'])[position()=3]")).click();

           driver.findElement(By.linkText("Demat Account")).click();
        }

           @Test(priority = 2)
           @Parameters({ "provider_name", "branch", "address", "clientId", "website", "UserName", "Password", "Dpid" })
          public void addDematAccount(String provider_name, String branch, String address, String clientId, String website,
            String UserName, String Password, String Dpid) {
        driver.findElement(By.xpath("//a[contains(@href, '#/app/DematAccount/Add')]")).click();

        driver.findElement(By.id("ProviderName")).sendKeys(provider_name);
        driver.findElement(By.id("Branch")).sendKeys(branch);
        driver.findElement(By.id("Address")).sendKeys(address);
        driver.findElement(By.id("ClientId")).sendKeys(clientId);
        driver.findElement(By.id("Website")).sendKeys(website);
        driver.findElement(By.id("UserName")).sendKeys(UserName);
        driver.findElement(By.id("Password")).sendKeys(Password);
        driver.findElement(By.id("DPId")).sendKeys(Dpid);

        driver.findElement(By.cssSelector("[value='Nsdl'][type='radio']")).click();



          WebElement element = driver.findElement(By.name("DrpDematAccountType"));
          Select se = new Select(element);
          se.selectByIndex(1);

           driver.findElement(By.cssSelector("[id='checkbank'][type='checkbox']")).click();
           driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
           driver.findElement(By.name("btn")).click();
           driver.findElement(By.className("confirm")).click();
        }

        @Test(priority = 3)
        @Parameters({ "provider_name", "branch", "address", "clientId", "website", "UserName", "Password", "Dpid" })
         public void addDematAcInvalid(String provider_name, String branch, String address, String clientId, String website,
         String UserName, String Password, String Dpid) {
        driver.findElement(By.xpath("//a[contains(@href, '#/app/DematAccount/Add')]")).click();

        driver.findElement(By.id("ProviderName")).sendKeys(provider_name);
        driver.findElement(By.id("Branch")).sendKeys(branch);
        driver.findElement(By.id("Address")).sendKeys(address);
        driver.findElement(By.id("ClientId")).sendKeys(clientId);
        driver.findElement(By.id("Website")).sendKeys(website);
        driver.findElement(By.id("UserName")).sendKeys(UserName);
        driver.findElement(By.id("Password")).sendKeys(Password);
        driver.findElement(By.id("DPId")).sendKeys(Dpid);

        driver.findElement(By.cssSelector("[value='Nsdl'][type='radio']")).click();

        // driver.findElement(By.cssSelector("[value='Cdsl'][type='radio']")).click();

          WebElement element =      driver.findElement(By.name("DrpDematAccountType"));
        Select se = new Select(element);
        se.selectByIndex(1);

         driver.findElement(By.cssSelector("[id='checkbank'][type='checkbox']")).click();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.findElement(By.name("btn")).click();
        driver.findElement(By.className("confirm")).click();
         }
      }

Demat.xml

     <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
     <suite name="Sample Test Suite" verbose="1" >

       <parameter name="username" value="shruti"/>
       <parameter name="password" value="shruti_123"/>
          <test name="Test Demat account" >
               <parameter name="provider_name" value="validkk"/>
               <parameter name="branch" value="barodakk"/>
               <parameter name="address" value="waghodia roadkk"/>
               <parameter name="clientId" value="1245787"/>
               <parameter name="website" value="www.goo.com"/>
               <parameter name="UserName" value="dishk"/>
               <parameter name="Password" value="1235467"/>
               <parameter name="Dpid" value="45487897"/>
       <classes>
       <class name="testngDemo.DematAccount">
        <methods>
        <exclude name="addDematAcInvalid"></exclude>
         <exclude name="deleteDematAccount" />
         <exclude name="logout" />
         </methods>
        </class>
       </classes>
      </test>


        <test name="Test Demat account with invalid data" >

         <!--  <parameter name="username" value="dishashah"/>
             <parameter name="password" value="disha1234"/>  -->
             <parameter name="provider_name" value="invalid"/>
             <parameter name="branch" value="baroda12@#"/>
             <parameter name="address" value="waghodia road12@"/>
             <parameter name="clientId" value="124578ghh"/>
             <parameter name="website" value="www.goocom"/>
             <parameter name="UserName" value="dish"/>
             <parameter name="Password" value="123546"/>
             <parameter name="Dpid" value="4548hj@789"/>
         <classes>
           <class name="testngDemo.DematAccount">
            <methods>
              <exclude name="addDematAccount"></exclude>
               <exclude name="deleteDematAccount" />
               <exclude name="logout" />
          </methods>
         </class>
      </classes>
     </test>

     </suite>

1 个答案:

答案 0 :(得分:0)

您需要将登录方法从Test方法更改为BeforeSuite方法。 e.g:

@BeforeSuite
@Parameters({ "username", "password" })
public void DematAccount1(String username, String password) {
    driver = Login.driver;
    lgn.login(username, password);
}

有关这些注释及其他注释的详细信息,请参阅TestNG Documentation - 2 - Annotations