2017-07-25 13 views
-2

我使用Selenium/Java/TestNG自動化Web應用程序。 應用程序在主頁面中有數據,並且正在從Excel表單中通過@dataProvider讀取數據。根據主頁中的一些標準,我想從同一個excel文件中的其他工作表中獲取數據,並將其傳遞到相同類中的相應@test中。嘗試了很多選擇,但無法找到合適的解決方案。如何使用DataProvider從多個Excel表中讀取值並將其傳遞給多個@test?

感謝很多提前, RESH

+3

您好!這個問題可能過於寬泛,無法產生任何有意義的答案。您可能想要將您的問題減少到您在嘗試執行此操作時遇到的特定問題,並參考[如何提問](https://stackoverflow.com/help/how-to-ask)。祝你好運! – mrfreester

+0

請參閱:[我如何做X?](https://meta.stackoverflow.com/questions/253069/whats-the-appropriate-new-current-close-reason-for-how-do-i-do- x)對SO的期望是,用戶提出問題不僅僅是研究來回答他們自己的問題,而且還分享研究,代碼嘗試和結果。這表明你已經花時間去嘗試幫助自己,它使我們避免重申明顯的答案,最重要的是它可以幫助你得到更具體和相關的答案!另見:[ask] – JeffC

回答

1

這裏有一種方式是可以做到這一點。

你基本上注入您的Excel電子表格中的「表」的名字到當前<test>標籤的情況下即,ITestContext作爲一個屬性,然後從@DataProvider註釋的數據提供者中,你基本上看這個屬性來決定哪些表具有被閱讀。

的下面的示例演示兩種@Test方法這樣做,其中,所述第一@Test方法注入該屬性作爲其一部分做一個流和所述第二@Test方法(它將不得不依賴於第一個),其現在由供電一個動態數據提供者只是消費數據。

import org.testng.ITestContext; 
import org.testng.Reporter; 
import org.testng.annotations.DataProvider; 
import org.testng.annotations.Test; 

public class SampleTestClass { 
    /** 
    * This method is simulating the master test method which would interact with 
    * the web app and bring the application to a state wherein the next test method 
    * should take over and interact. 
    */ 
    @Test 
    public void testLogin() { 
     //Simulating toggling between multiple flows. 
     int whichFlow = Integer.parseInt(System.getProperty("flow", "1")); 
     Reporter.getCurrentTestResult().getTestContext().setAttribute("flow", whichFlow); 
    } 

    /** 
    * This method is intentionally dependent on "testLogin" because its "testLogin" that will 
    * first interact with the web application and bring the application to the place from where 
    * further interaction is required, but which will vary based on some "x" criteria 
    * The "x" criteria is now available as an attribute in the current &lt;test&gt; tag's 
    * context. 
    */ 
    @Test(dependsOnMethods = "testLogin", dataProvider = "getData") 
    public void testSomethingElse(int a, String b) { 
     //Real test method logic goes here. 
     System.out.println(a + ":" + b); 

    } 

    @DataProvider 
    public Object[][] getData(ITestContext context) { 
     int whichFlow = Integer.parseInt(context.getAttribute("flow").toString()); 
     switch (whichFlow) { 
      case 1: 
       return new Object[][]{ 
         {1, "Login"}, 
         {2, "Signup"} 
       }; 
      case 2: 
       return new Object[][]{ 
         {100, "Fees"}, 
         {200, "Charges"} 
       }; 

      case 3: 
      default: 
       return new Object[][]{ 
         {900, "Logout"}, 
         {1000, "Random"} 
       }; 

     } 
    } 
} 
+0

非常感謝您的意見。這個概念正是我想在我的框架中實現的 – Reshmi

+0

如果能解決您的查詢,請您接受我的答案 –

0

只是一種方法,您可以嘗試複製或移動特定工作表到您的Excel工作表,更具體地說,在您的工作簿。

+0

我不想移動/複製工作表,因爲主工作表太大而無法維護。感謝建議 – Reshmi

相關問題