Есть 3 страницы
1. Домашняя страница - это страница по умолчанию
2. Страница входа - появляется при нажатии на ссылку для входа
3. Моя учетная запись - отображается после входа в систему
Примечание. Когда я использую нестатический Webdriver, он работает нормально до входа в систему, то есть успешно запускает домашнюю страницу, затем переходит на страницу входа и вводит учетные данные и успешно входит в систему для отображения страницы «Моя учетная запись»
Теперь после входа в систему, когда я пытаюсь получить доступ к[элемент заголовка страницы] на странице «Моя учетная запись» выдает исключение нулевого указателя [Указано в коде класса MyAccountPageObject]
Примечание: 1. При использовании кода статического веб-драйвера выполняется успешно, но исключение нулевого указателя на нестатическом веб-драйвере
2. Iсконструировал мой фреймворк Selenium-testng-maven аналогичным образом [с использованием нестатического веб-драйвера, и он работает нормально]
Ниже приведен весь код
HomePageObject
public class HomePageObject extends
TestBase{
public HomePageObject (WebDriver driverObj){
this.driverObj = driverObj;
}
public SignInPageObject navigateToSignInPage(){
System.out.println("In navigateToSignInPage");
driverObj.navigate().to("http://automationpractice.com/index.php?controller=authentication&back=my-account#account-creation");
SignInPageObject signInPageObj = PageFactory.initElements(driverObj, SignInPageObject.class);
return signInPageObj
}
}
SignInPageObject
public class SignInPageObject extends TestBase{
@FindBy(id = "email")
public static WebElement SignIn_Email_TB;
@FindBy(id = "passwd")
public static WebElement SignIn_Password_TB;
@FindBy(id = "SubmitLogin")
public static WebElement SignIn_Btn;
@FindBy(xpath = "//div[@id = 'center_column']/h1")
public static WebElement MyAccountHeading_Lnk;
public MyAccountPageObject cucumberValidateLogin(String emailAddress, String password) throws IOException{
System.out.println("In Cucumber Validatelogin method");
SignInPageObject.SignIn_Email_TB.sendKeys(emailAddress);
SignInPageObject.SignIn_Password_TB.sendKeys(password);
SignInPageObject.SignIn_Btn.click();
MyAccountPageObject myAccountPageObj = PageFactory.initElements(driverObj, MyAccountPageObject.class);
return myAccountPageObj;
}
}
Моя особенностьФайл
Feature: To test the Login functionality
Background:
Given Login page is displayed
Scenario: Title, Heading, Subheading and Breadcrumb validation on SignIn page
Then I Check the Presence of Page Title
"""
Login - My Store
"""
Scenario Outline: user login
When User enter valid "<EmailAddress>" and "<Password>" on Login Page , Clicking on Signin Button should display My Account Page
And user Closes the browser
Examples: Login credentials
| EmailAddress | Password |
| abcd@gmail.com | Qwertyui1 |
SignInTest
public class SignInTest extends
SignInPageObject{
HomePageObject homeObj;
MyAccountPageObject myAccountPageObj;
SignInPageObject signInPageObj;
String actualMyAccountpageHeading;
@Before()
public void before() throws Exception {
System.out.println("navigate to Homepage");
homeObj = gm_OpenApp("CH", "http://automationpractice.com/index.php"); //This method in testbase
}
@After()
public void after() throws Exception {
driverObj.quit();
}
@Given("^Login page is displayed$")
public void Login_page_is_displayed(){
System.out.println("*******Now navigate to login page*******");
signInPageObj = homeObj.navigateToSignInPage();
System.out.println("*******Login page is in display*******");
};
@Then("^I Check the Presence of Page Title$")
public void I_Check_the_Presence_of_Page_Title(String text){
System.out.println(text);
System.out.println(driverObj.getTitle());
};
@When("^User enter valid \"([^\"]*)\" and \"([^\"]*)\" on Login Page, Clicking on Signin Button displays My Account Page$")
public void User_enter_valid_Email_and_password_on_login_page_Click_on_Signin_Button(String emailAddress, String password) throws IOException, InterruptedException{
myAccountPageObj = signInPageObj.cucumberValidateLogin(emailAddress, password);
WebElement myAccountPageheadingEle = myAccountPageObj.MyAccount_PageHeading_Txt;
//--> This above statement causing error null pointer exception for the webelement listed on MyAccountPageObject
//Assert code to follow
}
@Then("^user Closes the browser$")
public void user_Closes_the_browser() throws IOException, InterruptedException{
System.out.println("Closing Now");
};
MyAccountPageObject
public class MyAccountPageObject extends
SignInPageObject{
//MyAccountPageTitle_Txt
@FindBy(xpath = "//div[@id = 'center_column']/h1")
public static WebElement MyAccount_PageHeading_Txt; //WebElement1
//MyAccount_Breadcrumb_txt
@FindBy(xpath = "//span[@class = 'navigation_page']")
public static WebElement MyAccount_Breadcrumb_txt; //WebElement2
public MyAccountPageObject (){
System.out.println("My Account Page launched");
//--> This text gets printed when I Navigate to myAccount page but while accessing
//any of the abovve 2 webelements it throws null pointer exception
}
}
TestBase
public class TestBase extends SignInRunnerTest{
//==>> My Code is working when I extend testbase to runnertest .. this is
not good practice because
there will be many runnertests and testbase will be only1 so I dont want
to testbase to extend the runnertest..what modification I will have to
do.. trying my best but unable to achieve to conclusion..//
public HomePageObject gm_OpenApp(String BrowserName, String URL)throws Exception{
System.out.println("In gm_OpenAp Method");
gm_LaunchBrowser(BrowserName);
gm_OpenURL(URL);
System.out.println("Now Homepage is in display");
HomePageObject homeObj = PageFactory.initElements(driverObj, HomePageObject.class);
return homeObj;
}
}
SignInTestRunnerTest
@CucumberOptions(
strict = true,
features = { "src/test/resources/featurefiles/Autoprac/SignInTestA1.feature" },
glue = {"samplecucumberproject"},
plugin = {"html:target/cucumber-html-report"},
monochrome = true,
dryRun = false
)
public class SignInTestRunnerTest extends AbstractTestNGCucumberTests {
public WebDriver driverObj;
public static String Browser;
public static String URL;
@BeforeMethod(alwaysRun = true)
@Parameters({ "Browser", "URL"})
public void getBrowser(String Browser, String URL) throws Exception{
System.out.println("SignIn Get Browser");
this.Browser=Browser;
this.URL = URL;
}
}
TestNG
test name="Regression Test-CH"
<parameter name="Browser" value="CH" ></parameter>
<parameter name="URL" value="http://automationpractice.com/index.php"></parameter>
<classes>
<class name="testrunner.SignInTestRunnerTest"/>
</classes>
</test>