2015-11-25 74 views
0

我在Firefox中使用testng + selenium webdriver时遇到了奇怪的问题。 我有一个与数据提供者一起工作的测试。第一次测试运行正常。在测试结束后,我导航到上一页,以使第二次运行正常。但在第二次运行WebElement.click()打开windown在新标签而不是相同的标签这就是为什么webdriver找不到其他元素。请给出任何建议如何解决这个问题!我试过: 1.使用Thread.Sleep。认为它会有所帮助,但睡眠时间后,新的ta仍然打开 2.使用windows.Handle切换到新选项卡。它也没有帮我,要素仍然没有找到WebElement.click()打开新标签,而不是在同一个标​​签中工作

我的代码如下(数据提供程序使用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)} 
    }; 
} 

}

回答

0

你是什么点击WebDriver(链接,按钮?)。 链接可能具有可使浏览器在新标签页/窗口中打开链接的“目标”属性。

+0

我的意思是这个WebElement createLetterButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(“.//*[@ id ='b-toolbar__left']/div/div/div [2]/div/a “))); createLetterButton.click();它是一个网站上的按钮 –

+0

什么是按钮处理程序(js函数)? –

+0

我不知道,就我所见,只有一个链接 –

0

根据您的xpath它是链接,并且UI显示为按钮链接。一些按钮链接通常会作为单独的新选项卡打开。你是否尝试过手动点击按钮链接? WebElement.click()函数只需单击该元素,而不是更改该功能以在同一选项卡或新选项卡中打开。通过手动点击进行检查。

0

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

相关问题