对象未使用PageFactory.initElements(driver,class);进行初始化;

时间:2019-05-15 13:09:30

标签: java selenium cucumber

在这里,我正在尝试初始化Homepage类对象,但是它不起作用

直到昨天,代码都在工作,但不知道出了什么问题,今天它就不工作了

  package StepDefnitions;

  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.support.PageFactory;

  import com.SHN.pages.AdviceAndPlanningPage;
  import com.SHN.pages.HomePage;

  import cucumber.api.java.en.Then;

  public class VerifyQuizSectionInAdviceAndPlanning {
public static WebDriver driver;


@Then("^click on Advice and Planning$")
public void ClickOnAdvice() throws InterruptedException{
    driver = Hooks.driver;

/ *此时代码不起作用* /

HomePage homepage1 = PageFactory.initElements(driver, HomePage.class);
    homepage1.ClickOnAdviceAndPlanning();
    Thread.sleep(3000);
}

@Then("^verify Quiz Section in Advice and planning$")
public void VerifyingQuizSection(){
    AdviceAndPlanningPage advicePlanning = PageFactory.initElements(driver, AdviceAndPlanningPage.class);
    advicePlanning.ClickingQuizButton();

}

主页类如下

   package com.SHN.pages;

   import java.util.ArrayList;
   import java.util.Arrays;
   import java.util.List;
   import java.util.Set;
   import java.util.concurrent.TimeUnit;

   import org.junit.Assert;
   import org.openqa.selenium.Keys;
   import org.openqa.selenium.WebDriver;
   import org.openqa.selenium.WebElement;
   import org.openqa.selenium.interactions.Actions;
   import org.openqa.selenium.support.FindAll;
   import org.openqa.selenium.support.FindBy;
   import org.openqa.selenium.support.How;
   import org.openqa.selenium.support.PageFactory;
   import org.openqa.selenium.support.ui.ExpectedConditions;
   import org.openqa.selenium.support.ui.WebDriverWait;







public class HomePage extends BaseClass {


public HomePage(WebDriver driver){
    super(driver);
}
public static String expectedCity = null;

@FindBy(how=How.CSS,using="[id*='divLearnAboutRight']")
WebElement LearnAboutTheRightCare;

@FindAll(@FindBy(how=How.CSS,using ="[class*='CareTypePadding']"))
List<WebElement> Caretypes;

@FindBy(how=How.CSS,using="[class*='CareTypePageHeading']")
WebElement Heading;

@FindBy(how=How.CSS,using="[class*='featuredHeader']")
WebElement FeaturedSeniorLiving;

@FindAll(@FindBy(how=How.CSS,using="[class*='featuredImage'] a"))
List<WebElement> FeaturedProperties;

@FindAll(@FindBy(how=How.CSS,using="[itemprop*='breadcrumb'] a"))
List<WebElement> LDPBreadcrumbs;

@FindBy(how=How.CSS,using="[id*='PopularSeniorLivingCities'] [class*='HeaderStylePopularCities']")
WebElement PopularSeniorLiving;

@FindAll(@FindBy(how=How.CSS,using ="[class*='slick-active']"))
List<WebElement> CitiesInsidePopularSenior;

@FindBy(how=How.CSS,using="[aria-label='Previous Arrow']")
WebElement LeftArrow;

@FindBy(how=How.CSS,using="[aria-label='Next Arrow']")
WebElement RightArrow;

@FindBy(how=How.XPATH,using="//*[@id='searchHomeLocation']")
WebElement SearchBoxCity;

@FindBy(how=How.XPATH,using="//*[@class='mui-pt-bd']")
WebElement citySuggestiveBox;

@FindBy(how=How.XPATH,using="//*[@class='mui-pt-bd']/ul/li/span")
WebElement FirstOptionInCitySuggestiveBox;

@FindBy(how=How.CSS,using="[class*='See-Homes']")
WebElement Search;

@FindBy(how=How.CSS,using="[id='errorMsg'] [class='SearchErrormsgPadding']")
WebElement ErrorMessage;

@FindBy(how=How.XPATH,using="//*[@id='SeniorLivingOption']")
WebElement AllSeniorLivingOptions; 

@FindBy(how=How.CSS,using="[class*='CareTypedropDownCont']")
WebElement CareTypesOptions;

@FindAll(@FindBy(how=How.CSS,using="[id*='chkCT']"))
List<WebElement> TypeofCareTypesAvailable;

@FindBy(how=How.CSS,using="[class*='advicePad80 Advicepad0']")
WebElement NewInAdvicePlanningSection;

@FindBy(how=How.CSS,using="[id*='senioradviceContent']")
WebElement NewInAdvicePlanningHeader;

@FindAll(@FindBy(how=How.CSS,using="[class*='seniorAdvicetext'] [title]"))
List<WebElement> NIAParticleTitle;

@FindBy(how=How.CSS,using="[class*='nav navbar-nav']")
WebElement Header;

@FindBy(how=How.CSS,using="[class*='nav navbar-nav'] [data-omtag*='header-menu:assisted-living']")
WebElement AssistedLivingHeaderLink;

@FindBy(how=How.CSS,using="[class*='nav navbar-nav'] [data-omtag*='header-menu:alzheimers-care']")
WebElement AlzheimerscareHeader;

@FindBy(how=How.CSS,using="[class*='nav navbar-nav'] [data-omtag*='header-menu:more-senior-living-options']")
WebElement moreSeniorLivingOptions;

@FindBy(how=How.CSS,using="[class*='nav navbar-nav'] [data-omtag*='header-menu:advice-planning']")
WebElement advicePalnningHeader;

@FindAll(@FindBy(how=How.CSS,using="[class*='nav navbar-nav col-lg-12 col-md-12 col-sm-12 col-xs-12 rmvpad'] a"))
List<WebElement> HeaderLinks;

@FindBy(how=How.XPATH,using="//*[@class='nav navbar-nav col-lg-12 col-md-12 col-sm-12 col-xs-12 rmvpad']/li[4]/a")
WebElement AdviceAndPlanning;




ArticlesPage articlespage = PageFactory.initElements(driver, ArticlesPage.class);
AssistedLivingPage assistedLiving = PageFactory.initElements(driver, AssistedLivingPage.class);
AlzheimersCarePage alzheimersCare = PageFactory.initElements(driver, AlzheimersCarePage.class);
MoreSeniorLivingOptionsPage moreSenior = PageFactory.initElements(driver, MoreSeniorLivingOptionsPage.class);
AdviceAndPlanningPage advicePlanning = PageFactory.initElements(driver, AdviceAndPlanningPage.class);


public void findingLearnSectionOnHP() throws InterruptedException{
    if((LearnAboutTheRightCare).isDisplayed()){
        System.out.println("Learn about section is displaying");
        List<WebElement> caretypes = Caretypes;
        if(caretypes.size()==6){
            for(int i=0;i<caretypes.size();i++){
                caretypes = Caretypes;
                String Name = caretypes.get(i).getText();
                caretypes.get(i).click();
                Thread.sleep(5000);
                String h1 = Heading.getText();
                if(Name.equals("55+ Living")){
                    if(h1.equals("55+ Communities")){
                        System.out.println("Navigated to Valid Page : "+h1);
                        driver.navigate().back();
                    }
                    else{
                        System.out.println("Navigated to InValid Page for care type: "+h1);
                        Assert.assertTrue(false);
                    }

                }
                else if(Name.equals("Low-Income Affordable")){
                    if(h1.equals("Affordable Low Income Senior Housing")){
                        System.out.println("Navigated to Valid Page : "+h1);
                        driver.navigate().back();
                    }
                    else{
                        System.out.println("Navigated to InValid Page for care type: "+h1);
                        Assert.assertTrue(false);
                    }
                }
                else if(Name.equals("Alzheimer's Care")){
                    if(h1.equals("Alzheimer’s Care (or Dementia Care)")){
                        System.out.println("Navigated to Valid Page : "+h1);
                        driver.navigate().back();
                    }
                    else{
                        System.out.println("Navigated to InValid Page for care type: "+h1);
                        Assert.assertTrue(false);
                    }
                }
                else{
                    if(h1.contains(Name)){
                        System.out.println("Navigated to Valid Page : "+h1);
                        driver.navigate().back();
                    }
                    else{
                        System.out.println("Navigated to InValid Page for care type: "+h1);
                        Assert.assertTrue(false);
                    }
                }
                driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
            }
        }
        else{
            System.out.println("Number of care types are not equal to 6");
            Assert.assertTrue(false);
        }
    }
    else{
        System.out.println("Learn about section is not displaying");
        Assert.assertTrue(false);           
    }               
}

public void findingFeaturedLivingSection(){
    if(FeaturedSeniorLiving.isDisplayed()){
        System.out.println("Featured Senior Living Communities section is displaying");
        List<WebElement> featuredProperties = FeaturedProperties;
        if(featuredProperties.size()==3){
            List <String> ExpectedTitles = Arrays.asList("Overture Fair Ridge","Overture Flower Mound","Overture Buckhead South");
            featuredProperties = FeaturedProperties;
            for(int i=0;i<featuredProperties.size();i++){
                String Community = featuredProperties.get(i).getText();
                String Result = SplittingString(Community);
                System.out.println("Result is "+Result);
                System.out.println("Successfully captured community Name \n"+Community);
                String[] arrofString1 =Community.split("\\s|\\,|\\n",2);
                if(arrofString1[1].contains(ExpectedTitles.get(i))){
                    System.out.println("Names are matching as expected");
                    Actions Act1 = new Actions(driver);
                    Act1.keyDown(Keys.SHIFT).click(featuredProperties.get(i)).keyUp(Keys.SHIFT).build().perform();
                    Set<String> w = driver.getWindowHandles();
                    String windows[] = new String[w.size()];
                    int k=0;
                    for (String window : w) {
                        windows[k] = window;
                        k++;
                    }
                    driver.switchTo().window(windows[1]);
                    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);        
                    String Title = driver.getTitle();
                    System.out.println("Title of the community is "+Title) ;
                    List<WebElement> BreadCrumb = LDPBreadcrumbs;

                    if(Title.contains(Result)){
                        System.out.println(i+" "+Community+" Community name on LDP");
                    }
                    else if(BreadCrumb.get(1).getText().contains(Result)){
                        System.out.println(i+" Passed through breadcrumbs"+" "+Community+" Community name on LDP");
                    }
                    else{
                        System.out.println(i+" "+Community+" Community name on LDP");
                        Assert.assertTrue(false);
                    }
                    driver.close();
                    driver.switchTo().window(windows[0]);
                }

            }
        }
        else{
            System.out.println("Featured Senior Living Communities section is not there on HomePage");
            Assert.assertTrue(false);
        }
    }

}

public String SplittingString(String string){
    String[] arrofstring = string.split(",", 2);
    return arrofstring[0];
}

public void findingPopularSenior() throws InterruptedException{
    WebElement objectName;
    for(int k=0;k<2;k++){
        switch (k){
        case 0:
            objectName = LeftArrow;
            if(PopularSeniorLiving.isDisplayed()){
                System.out.println("Popular Senior Living Communities section is displaying");
                List<WebElement> DefaultcitiesInsidePopularSenior = CitiesInsidePopularSenior;
                List<String> list1 = new ArrayList<String>();
                for(int i=0; i<DefaultcitiesInsidePopularSenior.size(); i++){
                    list1.add(DefaultcitiesInsidePopularSenior.get(i).getText());                
                }
                objectName.click();
                System.out.println("Clicks on Left carousel in Popular senior Living section ");
                Thread.sleep(3000);
                List<WebElement> NextPopularSenior = CitiesInsidePopularSenior;
                List<String> list2 = new ArrayList<String>();
                for(int i=0; i<NextPopularSenior.size(); i++){
                    list2.add(NextPopularSenior.get(i).getText()); 

                }

                if((!(list1.get(0).equals(list2.get(0))))&&(list1.get(0).equals(list2.get(1)))){
                    System.out.println("Both Lists are not equal ,Hence Left carousel is working Fine");

                }

                else{
                    System.out.println("Both Lists are  equal ,Left carousel not working ,Code Error ************************");
                }
            }
            else{
                System.out.println("Popular Senior Living Communities section is not there on HomePage");
                Assert.assertTrue(false);
            }
            break;
        case 1:
            objectName = RightArrow;
            if(PopularSeniorLiving.isDisplayed()){
                System.out.println("Popular Senior Living Communities section is displaying");
                List<WebElement> DefaultcitiesInsidePopularSenior = CitiesInsidePopularSenior;
                List<String> list1 = new ArrayList<String>();
                for(int i=0; i<DefaultcitiesInsidePopularSenior.size(); i++){
                    list1.add(DefaultcitiesInsidePopularSenior.get(i).getText());                
                }
                objectName.click();
                System.out.println("Clicks on Right carousel in Popular senior Living section ");
                Thread.sleep(3000);
                List<WebElement> NextPopularSenior = CitiesInsidePopularSenior;
                List<String> list2 = new ArrayList<String>();
                for(int i=0; i<NextPopularSenior.size(); i++){
                    list2.add(NextPopularSenior.get(i).getText()); 

                }

                if((!(list1.get(4).equals(list2.get(4))))&&(list1.get(4).equals(list2.get(3)))){
                    System.out.println("Both Lists are not equal ,Hence Right carousel is working Fine");

                }

                else{
                    System.out.println("Both Lists are  equal ,Right carousel not working,Code Error ************************");
                }
            }
            else{
                System.out.println("Popular Senior Living Communities section is not there on HomePage");
                Assert.assertTrue(false);
            }
            break;
        }
    }


}

public String enteringCityName(String city){

    try{    
        String functionName = "FindingSearchTextBox";

        if(SearchBoxCity.isDisplayed()){
            SearchBoxCity.sendKeys(city);
            WebDriverWait wait = new WebDriverWait(driver, 15);
            wait.until(ExpectedConditions.visibilityOf(citySuggestiveBox));
            if(citySuggestiveBox.isDisplayed()){
                System.out.println("Able to see city Autosuggestion box");
                String FirstOptionInAutoSuggestion = FirstOptionInCitySuggestiveBox.getText();                  
                SearchBoxCity.sendKeys(Keys.TAB);
                expectedCity = SearchBoxCity.getAttribute("value");
                System.out.println("Text inside search is "+expectedCity);
                if(expectedCity.equals(FirstOptionInAutoSuggestion)){
                    System.out.println("Selected city is equal to  Expected");
                    /*Driver.globalValues.put(Thread.currentThread().getName() + "expectedCity", expectedCity);*/
                    return expectedCity;

                }
                else{
                    System.out.println("Selected city is not equal to  Expected");
                    Assert.assertTrue(false);
                }
                System.out.println("Able to see search text box "+ functionName);

            }
            else{
                System.out.println("Not Able to see city Autosuggestion box");
                System.out.println("Entered invalid city name which is not exist in the system "+city);
            }
        }

        else{
            System.out.println("Not able to see search text box "+ functionName);   
            Assert.assertTrue(false);
        }

    }
    catch(Exception e){
        System.out.println("Exception occurs"+e.getMessage());
    }
    return expectedCity;
}

public void clickingOnSearch() throws InterruptedException{
    Search.click(); 
    Thread.sleep(3000);

}

public void verifyingErrorMsgDisplaying(){
    try{
        if(ErrorMessage.isDisplayed()){
            System.out.println("ErrorMessage is Displaying");
            String errorMessage = ErrorMessage.getText();
            if(errorMessage.contains("?")&&!errorMessage.contains("'")){
                System.out.println("ErrorMessage Contains '?' Mark");
                Assert.assertTrue(false);
            }
            else if(errorMessage.contains("'")){
                System.out.println("ErrorMessageContains apostrophe");
            }
        }
        else{
            System.out.println("ErrorMessage is not Displaying");
            Assert.assertTrue(false);
        }
    }
    catch(Exception e){
        System.out.println("Error message is "+e.getMessage());
    }
}

public void verifyingAllSeniorLivingOptions() throws InterruptedException{
    if(AllSeniorLivingOptions.isDisplayed()){
        AllSeniorLivingOptions.click();
        Thread.sleep(3000);
        WebDriverWait wait = new WebDriverWait(driver, 15);
        wait.until(ExpectedConditions.visibilityOf(CareTypesOptions));
        if(CareTypesOptions.isDisplayed()){
            System.out.println("Able to see options under All SeniorLiving Options");
            List<WebElement> careTypeAvailable = TypeofCareTypesAvailable;
            for(int i=0;i<careTypeAvailable.size();i++){
                if(careTypeAvailable.get(i).isDisplayed()){
                    careTypeAvailable.get(i).click();
                    System.out.println("able to select checkboxes for "+careTypeAvailable.get(i).getText());
                }
                else{
                    System.out.println("Not able to select checkboxes for "+careTypeAvailable.get(i).getText());
                }
            }
        }
        else{
            System.out.println("caretypes Options not available under All senior Living options dropdown");
            Assert.assertTrue(false);
        }

    }
}

public void verifyNoErrorMessage(){
    try{
        if(!(ErrorMessage.isDisplayed())){
            System.out.println(" Error Message is not Displaying");

        }
        else{
            System.out.println("Error message is displaying ");
            Assert.assertTrue(false);
        }
    }
    catch(Exception e){
        System.out.println("Error message is "+e.getMessage());
    }
}

public void AdviceAndPlanning(){
    try{
        if(NewInAdvicePlanningSection.isDisplayed()){
            System.out.println("New in Advice and planning section displaying");
            if(NewInAdvicePlanningHeader.isDisplayed()){
                /*Adi_SHN_HP_NewInAdviceAndPlanning obj = new Adi_SHN_HP_NewInAdviceAndPlanning();
                obj.ScrollingTillElementVisible("NewInAdvicePlanningSection");*/
                System.out.println("New in Advice and Planning header displaying ");
                List<WebElement> articles = new ArrayList<WebElement>();
                articles = NIAParticleTitle;
                for(int i=0;i<articles.size();i++){
                    System.out.println("article title is "+articles.get(i).getText());
                    String Title = articles.get(i).getText();
                    Actions act =new Actions(driver);
                    act.keyDown(Keys.SHIFT).click(articles.get(i)).keyUp(Keys.SHIFT).build().perform(); 
                    Set<String> w = driver.getWindowHandles();
                    String windows[] = new String[w.size()];
                    int k = 0;
                    for (String window : w) {
                        windows[k] = window;
                        k++;
                    }
                    driver.switchTo().window(windows[1]);
                    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
                    String articlesPageTitle = articlespage.Title.getText();
                    System.out.println("Title is "+Title);
                    System.out.println("Title is "+articlesPageTitle);
                    if(Title.equalsIgnoreCase(articlesPageTitle)){
                        System.out.println("New advice articles navigating to valid pages ");
                    }
                    else{
                        System.out.println("New advice articles navigating to Invalid pages ");
                        Assert.assertTrue(false);
                    }
                    driver.close();
                    driver.switchTo().window(windows[0]);
                }
            }
            else{
                System.out.println("New in Advice section Header Name is NOT displaying ");
                Assert.assertTrue(false);
            }
        }
        else{
            System.out.println("New in Advice section is not displaying ");
            Assert.assertTrue(false);
        }
    }
    catch(Exception e){
        System.out.println("Error is "+e.getMessage());
    }

}

public void HeaderOnSHN(){
    try{
        if(Header.isDisplayed()){
            System.out.println("Displaying Header ");
            List<WebElement> LinksOnHeader = HeaderLinks;
            for(int i=0;i<LinksOnHeader.size();i++){
                String linkName = LinksOnHeader.get(i).getText();
                LinksOnHeader.get(i).click();
                Thread.sleep(3000);
                if(linkName.equals("Assisted Living")){
                    String Header =  assistedLiving.CareTypeHeader.getText();
                    if(linkName.equals(Header)){
                        System.out.println("Navigated to valid page for Header link  "+linkName);
                    }
                    else{
                        System.out.println("Navigated to Invalid Page for Header Link "+linkName);
                        Assert.assertTrue(false);
                    }
                }
                else if(linkName.equals("Alzheimer’s Care")){
                    String Header =  alzheimersCare.CareTypeHeader.getText();
                    if(Header.equals("Alzheimer’s Care (or Dementia Care)")){
                        System.out.println("Navigated to valid page for Header link  "+linkName);
                    }
                    else{
                        System.out.println("Navigated to Invalid Page for Header Link "+linkName);
                        Assert.assertTrue(false);
                    }
                }
                else if(linkName.equals("More Senior Living Options")){
                    String Header = moreSenior.MoreSeniorheader.getText();
                    if(Header.equals("Senior Housing and Care Types")){
                        System.out.println("Navigated to valid page for Header link  "+linkName);
                    }
                    else{
                        System.out.println("Navigated to Invalid Page for Header Link "+linkName);
                        Assert.assertTrue(false);
                    }
                }

                else if(linkName.equals("Advice & Planning")){
                    List<WebElement> BreadCrumblinks = advicePlanning.BreadCrumbs;
                    for(int u=0;u<BreadCrumblinks.size();u++){
                        System.out.println("BreadCrumb name is "+BreadCrumblinks.get(u).getText());
                        if(BreadCrumblinks.get(u).getText().equals("Advice & Planning")){
                            System.out.println("Navigated to valid page for Header link  "+linkName);
                            if(advicePlanning.ContentWidget.getText().equalsIgnoreCase("We're here to help.")){
                                System.out.println("Content Widget is displaying on Advice and Planning ");
                            }
                            break;
                        }
                        else if(u==BreadCrumblinks.size()){
                            System.out.println("Advice and Planning Header is not navigating to valid page");
                            Assert.assertTrue(false);

                        }
                    }                       
                }                   
                driver.navigate().back();
                Thread.sleep(3000);
            }
        }
        else{
            System.out.println("Not Displaying Header");
            Assert.assertTrue(false);
        }
    }
    catch(Exception e){
        System.out.println("Exception occurred. "+e.getMessage());
        Assert.assertTrue(false);
    }

}

public void ClickOnAdviceAndPlanning(){
    try{
        if(Header.isDisplayed()){
            System.out.println("Displaying Header ");
            if(AdviceAndPlanning.isDisplayed()){
                System.out.println("Advice and planning heading is displaying in Header");
                AdviceAndPlanning.click();

            }
            else{
                System.out.println("Advice and planning heading is displaying in Header");
                Assert.assertTrue(false);
            }
        }
        else{
            System.out.println("Not Displaying Header");
            Assert.assertTrue(false);
        }
    }
    catch(Exception e){
        System.out.println("Error Message is "+e.getMessage());
    }
}

}

请让我知道代码中的问题是什么?为什么我的代码在接下来的时间不起作用

HomePage homepage1 = PageFactory.initElements(driver, HomePage.class);

1 个答案:

答案 0 :(得分:0)

不是将'HomePage.class'作为'initElements'方法的第二个参数传递:

标题## Homepage homepage1 = PageFactory.initElements(driver,HomePage.class)

只需传递“ this”作为第二个参数: Homepage homepage1 = PageFactory.initElements(驱动程序,此)