我使用Selenium/Java/TestNG自動化Web應用程序。 應用程序在主頁面中有數據,並且正在從Excel表單中通過@dataProvider讀取數據。根據主頁中的一些標準,我想從同一個excel文件中的其他工作表中獲取數據,並將其傳遞到相同類中的相應@test中。嘗試了很多選擇,但無法找到合適的解決方案。如何使用DataProvider從多個Excel表中讀取值並將其傳遞給多個@test?
感謝很多提前, RESH
我使用Selenium/Java/TestNG自動化Web應用程序。 應用程序在主頁面中有數據,並且正在從Excel表單中通過@dataProvider讀取數據。根據主頁中的一些標準,我想從同一個excel文件中的其他工作表中獲取數據,並將其傳遞到相同類中的相應@test中。嘗試了很多選擇,但無法找到合適的解決方案。如何使用DataProvider從多個Excel表中讀取值並將其傳遞給多個@test?
感謝很多提前, RESH
這裏有一種方式是可以做到這一點。
你基本上注入您的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 <test> 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"}
};
}
}
}
非常感謝您的意見。這個概念正是我想在我的框架中實現的 – Reshmi
如果能解決您的查詢,請您接受我的答案 –
您好!這個問題可能過於寬泛,無法產生任何有意義的答案。您可能想要將您的問題減少到您在嘗試執行此操作時遇到的特定問題,並參考[如何提問](https://stackoverflow.com/help/how-to-ask)。祝你好運! – mrfreester
請參閱:[我如何做X?](https://meta.stackoverflow.com/questions/253069/whats-the-appropriate-new-current-close-reason-for-how-do-i-do- x)對SO的期望是,用戶提出問題不僅僅是研究來回答他們自己的問題,而且還分享研究,代碼嘗試和結果。這表明你已經花時間去嘗試幫助自己,它使我們避免重申明顯的答案,最重要的是它可以幫助你得到更具體和相關的答案!另見:[ask] – JeffC