请尝试 https://aelp.smartsparrow.com/learn/open/ad6bca3c4a9b4955bbb78b8f378752bc 这个链接
然后选择 a 到 d 所有的选项 然后 点击 next button
在第二页 会出现三个 tab 页的东西,是包含在一个 iframe 里面。
我能验证到 第一个 tab 页里面的内容,却取不到 tab 的 title。 而且 也无法点击 后面两个 tab。

谁能给点意见? 谢谢啦

ps: 肯定不是 我没有进入 iframe 导致的。 xpath 也没有问题。



package com.smartsparrow.automation.pages.viewer;

import org.openqa.selenium.JavascriptExecutor;
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.FindBy;
import org.openqa.selenium.support.How;

import com.smartsparrow.automation.utils.PageHelper;

public class DecisionScreenWithFeedback extends InchCaseMaker{
    public DecisionScreenWithFeedback(WebDriver driver){
        super(driver);
    }

    @FindBy(how = How.XPATH, using = "//iframe[@id = 'iframe_infoSim']")
    private WebElement iframe;

    @FindBy(how = How.XPATH, using = "//div[@class = 'tabView']//li[1]/a")   
    private WebElement firstOptionTab;

    @FindBy(how = How.XPATH, using = "//div[@class = 'tabView']//li[2]/a")
    private WebElement secondOptionTab;

    @FindBy(how = How.XPATH, using = "//div[@class = 'tabView']//li[3]/a")
    private WebElement thirdOptionTab;

    @FindBy(how = How.XPATH, using = "//div[@class = 'tabView']//li[4]/a")
    private WebElement FourthOptionTab;

    @FindBy(how = How.XPATH, using = "//div[@class = 'tab-content']//p[1]")
    private WebElement othersTabDescription;

    @FindBy(how = How.XPATH, using = "//div[@class = 'tab-content']//ul/li[1]")
    private WebElement othersOptionOne;

    @FindBy(how = How.XPATH, using = "//div[@class = 'tab-content']//ul/li[2]")
    private WebElement othersOptionTwo;

    @FindBy(how = How.XPATH, using = "//div[@class = 'tab-content']//ul/li[3]")
    private WebElement othersOptionThree;

    public String getTabTitle(int tabNumber){
        WebElement[] tabs = {firstOptionTab, secondOptionTab, thirdOptionTab, FourthOptionTab};
        driver.switchTo().frame(iframe);
        String tabName = tabs[tabNumber].getText();
        driver.switchTo().defaultContent();
        return tabName;
    }

    public String getOthersTabDes(){
        driver.switchTo().frame(iframe);
        String text = othersTabDescription.getText();
        driver.switchTo().defaultContent();
        return text;
    }

    public String getOthersOption(int lineNumber){
        WebElement[] options = {othersOptionOne, othersOptionTwo, othersOptionThree};
        driver.switchTo().frame(iframe);
        String option = options[lineNumber].getText();
        driver.switchTo().defaultContent();
        return option;
    }

    public void changeTab(int tabNumber){
        WebElement[] tabs = {firstOptionTab, secondOptionTab, thirdOptionTab, FourthOptionTab};
        driver.switchTo().frame(iframe);
        for(int i = 0; i < tabs.length; i++)
        {   
            PageHelper.waitPageLoad(driver, PageHelper.SHORT_TIME);
            System.out.println(tabs[i].isDisplayed());
        }
        tabs[tabNumber].click();
//      new Actions(driver).moveToElement(tabs[tabNumber]).click().perform();
        PageHelper.waitPageLoad(driver, PageHelper.SHORT_TIME);
        driver.switchTo().defaultContent();
    }





}

package com.smartsparrow.automation.viewer;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.smartsparrow.automation.pages.viewer.BasicDecisionScreen;
import com.smartsparrow.automation.pages.viewer.DecisionScreenWithFeedback;
import com.smartsparrow.automation.pages.viewer.InchFeedbackPanel;
import com.smartsparrow.automation.utils.PageHelper;

public class InchCaseMakerTest {
    private WebDriver driver;

    @Before
    public void setUp(){
        driver = new FirefoxDriver();
    }

    @After
    public void tearDown(){
//      driver.quit();
    }

    @Test
    public void testInchCaseMakerLesson(){
        BasicDecisionScreen lesson = new BasicDecisionScreen(driver);
        PageHelper.waitBasicDecisionMakerPageAppear(driver);
        lesson.addOption(PageHelper.optionOne);
        lesson.addOptionDescription(PageHelper.FIRST_LINE, "This is option one.");
        lesson.addOption(PageHelper.optionTwo);
        lesson.addOptionDescription(PageHelper.SECOND_LINE, "This is option two.");
        lesson.addOption(PageHelper.optionThree);
        lesson.addOptionDescription(PageHelper.THIRD_LINE, "This is option three.");
        lesson.addOption(PageHelper.optionFour);
        lesson.addOptionDescription(PageHelper.FORTH_LINE, "This is option four.");
        DecisionScreenWithFeedback feedbackpage = lesson.clickNextToFeedbackPage();
        PageHelper.waitPageLoad(driver, 4);
        feedbackpage.changeTab(PageHelper.SECOND_LINE);  

//      DecisionScreenWithFeedback feedbackpage = lesson.clickNextToFeedbackPage();
//      PageHelper.waitFeedbackDecisionScreenPageAppear(driver);
//      assertTrue(feedbackpage.getHeader().equals("Decision Info Header"));

    }
}

package com.smartsparrow.automation.pages.viewer;

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.FindBy;
import org.openqa.selenium.support.How;

import com.smartsparrow.automation.utils.PageHelper;

public class BasicDecisionScreen extends InchCaseMaker{
    public BasicDecisionScreen(WebDriver driver){
        super(driver);
        //driver.get(PageHelper.inchUrl);
        driver.get(PageHelper.newInch);
    }

    @FindBy(how = How.XPATH, using = "//div[@class = 'form-inline']/span[1]")
    private WebElement studentPrompt;

    @FindBy(how = How.XPATH, using = "//input")
    private WebElement studentOptionArea;

    @FindBy(how = How.XPATH, using = "//div[@class = 'answersContainer']//thead//th[1]")
    private WebElement answerViewTableSelectionHead;

    @FindBy(how = How.XPATH, using = "//div[@class = 'answersContainer']//thead//th[2]")
    private WebElement answerViewTableExplanationHead;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][1]/td[@class = 'decision']")
    private WebElement firstAnswerViewDecision;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][1]/td[@class = 'answer']//textarea")
    private WebElement firstAnswerViewExplanation;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][1]/td[@class = 'delete']//i")
    private WebElement firstAnswerViewDeleteIcon;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][2]/td[@class = 'decision']")
    private WebElement secondAnswerViewDecision;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][2]/td[@class = 'answer']//textarea")
    private WebElement secondAnswerViewExplanation;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][2]/td[@class = 'delete']//i")
    private WebElement secondAnswerViewDeleteIcon;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][3]/td[@class = 'decision']")
    private WebElement thirdAnswerViewDecision;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][3]/td[@class = 'answer']//textarea")
    private WebElement thirdAnswerViewExplanation;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][3]/td[@class = 'delete']//i")
    private WebElement thirdAnswerViewDeleteIcon;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][4]/td[@class = 'decision']")
    private WebElement forthAnswerViewDecision;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][4]/td[@class = 'answer']//textarea")
    private WebElement forthAnswerViewExplanation;

    @FindBy(how = How.XPATH, using = "//tr[@class = 'answerView'][4]/td[@class = 'delete']//i")
    private WebElement forthAnswerViewDeleteIcon;

    @FindBy(how = How.XPATH, using = "//td[@class = 'delete']//h4")
    private WebElement deleteOptionTitle;

    @FindBy(how = How.XPATH, using = "//td[@class = 'delete']//button[1]")
    private WebElement deleteOptionButton;

    @FindBy(how = How.XPATH, using = "//td[@class = 'delete']//button[2]")
    private WebElement cancelDeleteOptionButton;

    @FindBy(how = How.XPATH, using = "//div[@class = 'menuHolder']//li[1]/a")
    private WebElement selection;

    @FindBy(how = How.XPATH, using = "//iframe[@id = 'iframe_decisionSim']")
    private WebElement iframe;

    public String getPrompt(){
        driver.switchTo().frame(iframe);
        String prompt = studentPrompt.getText();
        driver.switchTo().defaultContent();
        return prompt;
    }

    public String getTableSelctionHead(){
        driver.switchTo().frame(iframe);
        String selectionHeader = answerViewTableSelectionHead.getText();
        driver.switchTo().defaultContent();
        return selectionHeader;
    }

    public String getTableExplanationHead(){
        driver.switchTo().frame(iframe);
        String explanationHeader = answerViewTableExplanationHead.getText();
        driver.switchTo().defaultContent();
        return explanationHeader;
    }

    public void clickDeleteIcon(){
        driver.switchTo().frame(iframe);
        firstAnswerViewDeleteIcon.click();
        PageHelper.waitPageLoad(driver, PageHelper.SHORT_TIME);
        driver.switchTo().defaultContent();
    }

    public void addOption(int optionNumber){
        String[] optionNames = {"Option One", "Option Two", "Option Three", "Option Four", "Option Five"};
        driver.switchTo().frame(iframe);
        studentOptionArea.sendKeys(optionNames[optionNumber]);
        PageHelper.waitPageLoad(driver, PageHelper.SHORT_TIME);
        studentOptionArea.sendKeys(Keys.DOWN);
        selection.click();
        PageHelper.waitPageLoad(driver, PageHelper.SHORT_TIME);
        driver.switchTo().defaultContent();
    }

    public void deleteOption(){
        driver.switchTo().frame(iframe);
        firstAnswerViewDeleteIcon.click();
        PageHelper.waitPageLoad(driver, PageHelper.SHORT_TIME);
        deleteOptionButton.click();
        driver.switchTo().defaultContent();
    }

    public String getDeletOptionWindowTitle(){
        driver.switchTo().frame(iframe);
//      firstAnswerViewDeleteIcon.click();
//      PageHelper.waitPageLoad(driver, PageHelper.SHORT_TIME);
        String title = deleteOptionTitle.getText();
        driver.switchTo().defaultContent();
        return title;
    }

    public String getDeleteOptionWindowButtonText(){
        driver.switchTo().frame(iframe);
//      firstAnswerViewDeleteIcon.click();
//      PageHelper.waitPageLoad(driver, PageHelper.SHORT_TIME);
        String text = deleteOptionButton.getText();
        driver.switchTo().defaultContent();
        return text;
    }

    public void clickCancelDeleteOption(){
        driver.switchTo().frame(iframe);
//      new Actions(driver).moveToElement(cancelDeleteOptionButton).click().perform();
        cancelDeleteOptionButton.click();
        cancelDeleteOptionButton.click();
        driver.switchTo().defaultContent();
    }

    public String getDeleteOptionWindowCancelText(){
        driver.switchTo().frame(iframe);
        firstAnswerViewDeleteIcon.click();
        PageHelper.waitPageLoad(driver, PageHelper.SHORT_TIME);
        String text = cancelDeleteOptionButton.getText();
        driver.switchTo().defaultContent();
        return text;
    }

    public void addOptionDescription(int optionLine, String des){
        WebElement[] optionDescriptions = {firstAnswerViewExplanation, secondAnswerViewExplanation, thirdAnswerViewExplanation, forthAnswerViewExplanation};
        driver.switchTo().frame(iframe);
        optionDescriptions[optionLine].click();
        optionDescriptions[optionLine].sendKeys(des);
        answerViewTableExplanationHead.click();
        driver.switchTo().defaultContent();
    }

    public String getDecisionText(int optionLine){
        WebElement[] decisions = {firstAnswerViewDecision, secondAnswerViewDecision, thirdAnswerViewDecision, forthAnswerViewDecision};
        driver.switchTo().frame(iframe);
        String text = decisions[optionLine].getText();
        driver.switchTo().defaultContent();
        return text;
    }

    public String getDecisionDescriptionText(int optionLine){
        WebElement[] descriptions = {firstAnswerViewExplanation, secondAnswerViewExplanation, thirdAnswerViewExplanation, forthAnswerViewExplanation };
        driver.switchTo().frame(iframe);
        String text = descriptions[optionLine].getText();
        driver.switchTo().defaultContent();
        return text;
    }

    public DecisionScreenWithFeedback clickNextToFeedbackPage(){
        nextButton.click();
        PageHelper.waitQuestionChange(driver);
        return new DecisionScreenWithFeedback(driver);
    }

}


↙↙↙阅读原文可查看相关链接,并与作者交流