WebElement.click()打开新选项卡,而不是在同一选项卡中工作

时间:2015-11-25 06:57:03

标签: java selenium-webdriver testng

我在Firefox中使用testng + selenium webdriver时遇到了奇怪的问题。 我有一个与dataprovider一起使用的测试。首次测试运行正常。在测试结束时,我导航到上一页以使第二次运行正常。但是在第二次运行时,WebElement.click()会在 NEW TAB 中打开,而不是相同的选项卡这就是为什么webdriver无法找到其他元素。请提供任何建议如何解决这个问题!

我试过了: 1.使用Thread.Sleep。认为这会有所帮助,但在睡眠时间后,新的ta仍会打开 2.使用windows.Handle切换到新选项卡。它也没有帮助我,仍然没有找到元素

我的代码如下(Dataprovider使用Sax从xml获取数据,这可以正常工作):

package com.epam.training.selenium;

import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import javax.xml.parsers.ParserConfigurationException;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import utils.TestingUtils;
import xmlparser.TestingData;

public class SeleniumTest 
{
    private WebDriver driver;
    private String mainPage = "https://mail.ru";
    private String inboxPage = "https://e.mail.ru/messages/inbox/?back=1";
    private String loginVal = "seleniumtest";
    private String passwordVal = "Qwerty123";
    private String domainVal = "@inbox.ru";


@BeforeClass
public void startBrowser(){
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get(mainPage);
}

@Test(groups = "smoke" , priority=1)
public void authTest(){

    WebElement login = driver.findElement(By.xpath(".//*[@id='mailbox__login']" ));
    login.sendKeys(loginVal);

    WebElement password = driver.findElement(By.xpath(".//*[@id='mailbox__password']"));
    password.sendKeys(passwordVal);

    Select selectDomain = new Select(driver.findElement(By.xpath(".//*[@id='mailbox__login__domain']")));
    selectDomain.selectByVisibleText(domainVal);

    WebElement authButton = driver.findElement(By.xpath(".//*[@id='mailbox__auth__button']"));
    authButton.click();

    WebElement logoutLink = driver.findElement(By.xpath(".//*[@id='PH_logoutLink']"));
    boolean expected = true;
    Assert.assertEquals(logoutLink.isDisplayed(), expected);
}


@Test(groups = {"drafts test"}, dependsOnGroups = "smoke", dataProvider = "New letter test data")
public void createNewLetter(TestingData data){


    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(3, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);

    WebElement createLetterButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='b-toolbar__left']/div/div/div[2]/div/a")));
    createLetterButton.click();

    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");

    WebElement senderAdress = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='compose__header__content']/div[2]/div[2]/div[1]/textarea[2]")));

    senderAdress.sendKeys(data.getSenderAdress());

    WebElement senderSubject = driver.findElement(By.xpath(".//*[@class='compose__header__field']"));
    Actions builder = new Actions(driver);
    builder.sendKeys(senderSubject, data.getSenderSubject()).click().perform();

    WebElement disableInteractionsButton = driver.findElement(By.xpath("html/body/div[2]/div/div[5]/div/div/div/div/div/div/div/div/div/div/div/div/div/div[8]/div[2]/div[2]/div[2]/div/div/form/div[2]/div[4]/div[1]/div[2]/table/tbody/tr/td/table[1]/tbody/tr/td[19]/a/span[1]/span[2]"));
    disableInteractionsButton.click();

    WebElement senderText = driver.findElement(By.xpath("html/body/div[2]/div/div[5]/div/div/div/div/div/div/div/div/div/div/div/div/div/div[8]/div[2]/div[2]/div[2]/div/div/form/div[2]/div[4]/div[3]/table/tbody/tr/td[1]/div/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[2]/td/textarea"));

    builder.moveToElement(senderText).click().keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0061'))
    .keyUp(Keys.CONTROL).sendKeys(data.getSenderText()).keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0073')).perform();

    boolean expected = true;
    //      Assert.assertEquals(senderAdress.isDisplayed(), expected);

    WebElement draftsLink = wait.until(new ExpectedCondition<WebElement>() {
        public WebElement apply(WebDriver d) {
            return d.findElement(By.xpath(".//div[@id='b-toolbar__right']/div/div/div[2]/div[6]/div"));
        }
    });
    expected = true;
    Assert.assertEquals(draftsLink.isEnabled(), expected);
    driver.get(inboxPage); 
    try { 
        Alert alert = driver.switchTo().alert();
        alert.accept();
    }
    catch (NoAlertPresentException e) {

    }
}


@DataProvider(name = "New letter test data")
public Object[][] getValuesForNewDraft() throws ParserConfigurationException, SAXException, IOException{
    List<TestingData> dataList = TestingUtils.parse();
    return new Object[][]{
        new Object[] {dataList.get(0)},
        new Object[] {dataList.get(1)}
    };
}

}

3 个答案:

答案 0 :(得分:0)

您使用WebDriver点击了什么(链接,按钮?)。 链接可能有&#39;目标&#39;可以使浏览器在新标签/窗口中打开链接的属性。

答案 1 :(得分:0)

根据您的xpath链接,以及显示为按钮链接的用户界面。某些按钮链接通常会作为单独的新选项卡打开。您是否尝试过手动点击按钮链接? WebElement.click()函数它只是单击该元素,而不是更改该函数以在同一选项卡或新选项卡中打开。通过手动点击来检查它。

答案 2 :(得分:0)

问题解决了! 我有最新的Firefox build v.42,Selenium不完全支持。 安装v.31的Firefox解决了所有问题。谢谢你的回答!

相关问题