英文:
Method requires a @DataProvider, even when @DataProvider for @Test method is provided
问题
Here's the translated code without the translation of code-related parts:
**Tasks:**
*Take a look at Test scenario 5!*
```import installations.APKInstallation;
import installations.AppDeletion;
import utils.ColorFormatter;
import utils.tempUser;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.*;
import screens.*;
import utils.TestProperties;
import java.lang.*;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import dataobjects.Password;
import dataobjects.User;
import static dataobjects.Email.getEmail;
import static dataobjects.Email.getErrorMessage;
public class Tasks {
private void printTestResult(int testCaseNumber) {
ColorFormatter.colorize("Test " + testCaseNumber + " Passed!",ColorFormatter.boldGreenColor);
}
private AppDeletion appDeletion = new AppDeletion();
private APKInstallation apkInstallation = new APKInstallation();
private AndroidDriver driver;
public WelcomeScreen welcomeScreen;
public SignUpScreen signUpScreen;
public ConfirmRegistration confirmRegistration;
public AddToCart addToCart;
public LogOutAfterSignUp logOutAfterSignUp;
public InvalidCredentialsScreen invalidCredentialsScreen;
public NotificationScreen notificationScreen;
public int testCaseNumber;
public String tempEmail= tempUser.unTempEmail; //For creating manual account
public String tempPassword=tempUser.unTempPassword;//For creating manual account
@BeforeMethod(description="Starts each session",groups=("StartUp"),alwaysRun = true)
public void setUp() throws IOException, InterruptedException {
// Close System.err to prevent any error messages from being printed to the console
System.err.close();
// Set System.err to the default output stream to allow any error messages to be printed to the console again
System.setErr(System.out);
// Installs application
apkInstallation.installAPK();
// Set the desired capabilities for the test
DesiredCapabilities desiredCapabilities = TestProperties.setDesiredCapabilities();
desiredCapabilities.setCapability("appium:udid", "emulator-5554");
// Set the remote URL for Appium
URL remoteUrl = new URL(TestProperties.getProperty("appiumURL"));
// Create a new AndroidDriver instance with the desired capabilities and remote URL
driver = new AndroidDriver(remoteUrl, desiredCapabilities);
// Set the implicit wait timeout to 15 seconds
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Initialize the page objects for the different screens of the app
welcomeScreen = new WelcomeScreen(driver);
signUpScreen = new SignUpScreen(driver);
confirmRegistration = new ConfirmRegistration(driver);
addToCart = new AddToCart(driver);
logOutAfterSignUp = new LogOutAfterSignUp(driver);
invalidCredentialsScreen = new InvalidCredentialsScreen(driver);
notificationScreen = new NotificationScreen(driver);
// Reset the app to the initial state before each test
driver.resetApp();
}
@Test(description = "User installs app from the APK file", groups = {"App Download","TC1"}, enabled = true)// Done
public void appDownload_1(){
testCaseNumber = 1;
apkInstallation.testAppDownload(testCaseNumber);
printTestResult(testCaseNumber);
}
@Test(description = "App works correctly after installation", groups = {"App Download","TC2"}, enabled = true)// Done
public void appDownload_2(){
testCaseNumber = 2;
notificationScreen.validatedNotificationButton(testCaseNumber);
notificationScreen.clickNotificationButton(testCaseNumber);
//Clicks on button after verification is successful (true)
//Verify application functioning by clicking button after launch
printTestResult(testCaseNumber);
}
@Test(description = "App can be uninstalled from the device", groups = {"App Uninstallation","TC3"}, enabled = true) //Done
public void appUninstallation_3() throws IOException, InterruptedException {
testCaseNumber = 3;
appDeletion.isPackageInstalled(testCaseNumber);
appDeletion.uninstallation(testCaseNumber); // Return results of app deletion
printTestResult(testCaseNumber);
}
@Test(description = "User navigates to Registration page", groups = {"Account Registration", "TC4"}, enabled = true)
public void accountRegistration_4(){
testCaseNumber = 4;
notificationScreen.validatedNotificationButton(testCaseNumber);
notificationScreen.clickNotificationButton(testCaseNumber);
welcomeScreen.clickSignUp(testCaseNumber);
//Confirm user is navigated to Sign Up page
signUpScreen.verifySignUpScreen(testCaseNumber);
printTestResult(testCaseNumber);
}
@Test(description = "User is not able to register Account with invalid Email", groups = {"Account Registration", "TC5"}, dataProviderClass = dataobjects.Data.class, dataProvider = "invalid-email", enabled = true)
public void accountRegistration_5(Email email){
testCaseNumber = 5;
notificationScreen.validatedNotificationButton(testCaseNumber);
notificationScreen.clickNotificationButton(testCaseNumber);
welcomeScreen.clickSignUp(testCaseNumber);
signUpScreen.AcceptTerms(testCaseNumber);
signUpScreen.fillInvalidEmail(email.getEmail(), email.getErrorMessage(), testCaseNumber);
// Other test logic here
}
@AfterMethod(description="Runs after each test",groups=("CleanUp"),alwaysRun = true)
public void lastCleanUp() {
driver.quit();
}
}
FillInvalidEmail:
public static void fillInvalidEmail(String email, String errorMessage, int testCaseNumber) {
try {
Email.sendKeys(email);
ColorFormatter.colorize(errorMessage, ColorFormatter.orangeColor);
} catch (TestFailureException e) {
throw new TestFailureException(testCaseNumber);
}
ColorFormatter.colorize("\nPassed!", ColorFormatter.greenColor);
}
Email:
public class Email {
private static String email;
private static String errorMessage;
public Email(String email, String errorMessage) {
this.email = email;
this.errorMessage = errorMessage;
}
public static String getEmail() {
return email;
}
public static String getErrorMessage() {
return errorMessage;
}
@Override
public String toString() {
return "Email{" +
", email='" + email + '\'' +
", errorMessage='" + errorMessage + '\'' +
'}';
}
}
英文:
After adding multiple @DataProviders
for package dataobject
class UserData
, then renaming class to Data
, using newly added @DataProviders
called invalid-email
for testing multiple invalid inputs, results in error : Method public void Tasks.accountRegistration_5() requires a @DataProvider named : invalid-email
. What could cause the error?
It's allowed to use multiple @DataProviders
in same class.
Here is code:
Tasks:
Take a look at Test scenario 5!
import installations.AppDeletion;
import utils.ColorFormatter;
import utils.tempUser;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.*;
import screens.*;
import utils.TestProperties;
import java.lang.*;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import dataobjects.Password;
import dataobjects.User;
import static dataobjects.Email.getEmail;
import static dataobjects.Email.getErrorMessage;
public class Tasks {
private void printTestResult(int testCaseNumber) {
ColorFormatter.colorize("Test " + testCaseNumber + " Passed!",ColorFormatter.boldGreenColor);
}
private AppDeletion appDeletion = new AppDeletion();
private APKInstallation apkInstallation = new APKInstallation();
private AndroidDriver driver;
public WelcomeScreen welcomeScreen;
public SignUpScreen signUpScreen;
public ConfirmRegistration confirmRegistration;
public AddToCart addToCart;
public LogOutAfterSignUp logOutAfterSignUp;
public InvalidCredentialsScreen invalidCredentialsScreen;
public NotificationScreen notificationScreen;
public int testCaseNumber;
public String tempEmail= tempUser.unTempEmail; //For creating manual account
public String tempPassword=tempUser.unTempPassword;//For creating manual account
@BeforeMethod(description="Starts each session",groups=("StartUp"),alwaysRun = true)
public void setUp() throws IOException, InterruptedException {
// Close System.err to prevent any error messages from being printed to the console
System.err.close();
// Set System.err to the default output stream to allow any error messages to be printed to the console again
System.setErr(System.out);
// Installs application
apkInstallation.installAPK();
// Set the desired capabilities for the test
DesiredCapabilities desiredCapabilities = TestProperties.setDesiredCapabilities();
desiredCapabilities.setCapability("appium:udid", "emulator-5554");
// Set the remote URL for Appium
URL remoteUrl = new URL(TestProperties.getProperty("appiumURL"));
// Create a new AndroidDriver instance with the desired capabilities and remote URL
driver = new AndroidDriver(remoteUrl, desiredCapabilities);
// Set the implicit wait timeout to 15 seconds
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Initialize the page objects for the different screens of the app
welcomeScreen = new WelcomeScreen(driver);
signUpScreen = new SignUpScreen(driver);
confirmRegistration = new ConfirmRegistration(driver);
addToCart = new AddToCart(driver);
logOutAfterSignUp = new LogOutAfterSignUp(driver);
invalidCredentialsScreen = new InvalidCredentialsScreen(driver);
notificationScreen = new NotificationScreen(driver);
// Reset the app to the initial state before each test
driver.resetApp();
}
@Test(description = "User installs app from the APK file", groups = {"App Download","TC1"}, enabled = true)// Done
public void appDownload_1(){
testCaseNumber = 1;
apkInstallation.testAppDownload(testCaseNumber);
printTestResult(testCaseNumber);
}
@Test(description = "App works correctly after installation", groups = {"App Download","TC2"}, enabled = true)// Done
public void appDownload_2(){
testCaseNumber = 2;
notificationScreen.validatedNotificationButton(testCaseNumber);
notificationScreen.clickNotificationButton(testCaseNumber);
//Clicks on button after verification is successful (true)
//Verify application functioning by clicking button after launch
printTestResult(testCaseNumber);
}
@Test(description = "App can be uninstalled from the device", groups = {"App Uninstallation","TC3"}, enabled = true) //Done
public void appUninstallation_3() throws IOException, InterruptedException {
testCaseNumber = 3;
appDeletion.isPackageInstalled(testCaseNumber);
appDeletion.uninstallation(testCaseNumber); // Return results of app deletion
printTestResult(testCaseNumber);
}
@Test(description = "User navigates to Registration page", groups = {"Account Registration", "TC4"}, enabled = true)
public void accountRegistration_4(){
testCaseNumber = 4;
notificationScreen.validatedNotificationButton(testCaseNumber);
notificationScreen.clickNotificationButton(testCaseNumber);
welcomeScreen.clickSignUp(testCaseNumber);
//Confirm user is navigated to Sign Up page
signUpScreen.verifySignUpScreen(testCaseNumber);
printTestResult(testCaseNumber);
}
@Test(description = "User is not able to register Account with invalid Email", groups ={"Account Registration", "TC5"},dataProvider = "invalid-email",enabled = true)
public void accountRegistration_5(){
testCaseNumber = 5;
notificationScreen.validatedNotificationButton(testCaseNumber);
notificationScreen.clickNotificationButton(testCaseNumber);
welcomeScreen.clickSignUp(testCaseNumber);
signUpScreen.AcceptTerms(testCaseNumber);
signUpScreen.fillInvalidEmail(getEmail(), getErrorMessage(), testCaseNumber);
signUpScreen.clickCreateAccount(testCaseNumber);
}
@AfterMethod(description="Runs after each test",groups=("CleanUp"),alwaysRun = true)
public void lastCleanUp() {
driver.quit();}
}
FillInvalidEmail:
try {
Email.sendKeys(email);
ColorFormatter.colorize(errorMessage, ColorFormatter.orangeColor);
} catch (TestFailureException e) {
throw new TestFailureException(testCaseNumber);
}
ColorFormatter.colorize("\nPassed!", ColorFormatter.greenColor);
}
Email is MobileElement
Data:
import org.testng.annotations.DataProvider;
/**
* A class that provides test data for Registration tests.
*/
public class Data {
@DataProvider(name = "login-credentials")
public static Object[][] createUserData() {
return new Object[][]{
{new User("tiweti1200@otanhome.com", "Tiweti1200!", "Unregistered User")},
{new User("wqji321@gqwasd.com", "Warase3459!", "Unverified User")},
{new User("xoheto3034@ngopy.com", "qwekwei1932!", "Incorrect Password")}
};
}
@DataProvider(name = "invalid-password")
public static Object[][] createInvalidPassword() {
return new Object[][]{
{new Password("abcd")},//Less than 8 characters, no upper case letters, no digits, no special characters
{new Password("QWERT")},//Less than 8 characters, no lower case letters, no digits, no special characters
{new Password("qwertyui")},//No upper case letters, no digits, no special characters
{new Password("ASDFGHJK")},//No lower case letters, no digits, no special characters
{new Password("qwerty123")},///No upper case letters,no special characters
{new Password("ZXCVBX986")},//No lower case letters,no special characters
{new Password("QWERasdfg")},//No digits, no special characters
{new Password("FDWkwe!?")},//No digits
{new Password("qwekwei1932")},//No special characters
{new Password("QWsd12!")},//Less than 8 characters
{new Password("qwekwei1932!")},//No upper case
{new Password("ZXCAS1932!")},//No lower case
{new Password("!@£$%^&*")},//No upper,lower case letters, no digits
{new Password("123456789")},//No upper,lower case letters, no special characters
{new Password("@£%^1932!")},//No upper,lower case letters
{new Password("")},//No letters, no numbers, no characters
{new Password("A1!aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaa")}
};//More than 256 characters
}
@DataProvider(name = "invalid-email")
public static Object[][] createInvalidEmail() {
return new Object[][]{
{new Email("fitop40249asuflex.com", "Email with missing @:")},
{new Email("fitop40249@.com", "Email with missing domain @:")},
{new Email("invalidemail@com", "Email with missing dot:")},
{new Email("invalidemail@.", "Email with missing com:")},
{new Email("", "Email field empty:")},
{new Email("@asuflex.com", "Email is missing local part")},
{new Email("fitop40249@uflex.com", "Email has incorrect domain")},
{new Email("fitop40249@uflex,com", "Email has comma instead of dot")},
{new Email("invalidemail@.comqwe", "Email has extra characters after com")},
{new Email("invali demail@.com", "Email contains 'Space' character")}
};
}
}
Email:
public class Email {
private static String email;
private static String errorMessage;
public Email(String email, String errorMessage) {
this.email = email;
this.errorMessage = errorMessage;
}
public static String getEmail() {
return email;
}
public static String getErrorMessage() {return errorMessage;}
@Override
public String toString() {
return "Email{" +
", email='" + email + '\'' +
", errorMessage='" + errorMessage + '\'' +
'}';
}
}
Commenting out that specific Test or using another @DataProvider
doesn't resolve issue, not using @DataProvider
results in test running succesfully , ofcourse without fillInvalidEmail
Tried to use multiple @DataProviders
for multiple Testing scenarios inside Tasks
, results in TestNGException
which tells that used @DataProvider
is required, even when it is mentioned in @Test
notation.
Update:
Changing name of @DataProvider
did not fix error;
Removing space in increateInvalidEmail()
did not fix error
Solution:
When using Inherited @DataProvider
from different class , you must provide dataProviderClass
in @Test annotation for Inherited @DataProvider
to work.
Another minor issue:
Tasks.accountRegistration_5()] has no parameters defined but was found to be using a data provider (either explicitly specified or inherited from class level annotation).
To fix this change 2 things:
import static dataobjects.Email.getEmail;
import static dataobjects.Email.getErrorMessage;
replace with :
import dataobjects.Email;
- Pass it as parameter to accountRegistration_5() method
Like this:
@Test(description = "User is not able to register Account with invalid Email",
groups ={"Account Registration", "TC5"},
dataProviderClass = dataobjects.Data.class,
dataProvider = "invalid-email"
,enabled = true)
public void accountRegistration_5(Email email){
signUpScreen.fillInvalidEmail(Email.getEmail(), Email.getErrorMessage(), testCaseNumber);
答案1
得分: 1
你已在不同的类中定义了数据提供程序。因此,你还应该在@Test
注解中作为参数指定该类,以及数据提供程序的名称。像这样:
@Test(description = "用户无法使用无效的电子邮件地址注册帐户",
groups = {"帐户注册", "TC5"},
dataProvider = "invalid-email",
dataProviderClass = your.package.Data.class, // <-- 在这里放入你的Data类
enabled = true)
英文:
You have data provider defined in different class. So you should also specify that class as a parameter to @Test
annotation along with data provider name. Like this:
@Test(description = "User is not able to register Account with invalid Email",
groups ={"Account Registration", "TC5"},
dataProvider = "invalid-email",
dataProviderClass = your.package.Data.class, // <-- put your Data class here
enabled = true)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论