2010-07-19 9 views
6

Algunos enlaces en nuestra página se abren en una nueva ventana usando target = "_ blank". ¿Cómo puedo hacer que el selenio mire en la ventana derecha para poder verificar que la página se está enlazando a la página correcta?¿Cómo verificar un enlace target = "_ blank" usando selenio?

Aquí es lo que he estado intentando:

open    /page/ 
click    link=Find us on Facebook! 
pause    2000 
selectWindow  title=window title 
verifyTextPresent some text 
+1

No estoy familiarizado con el selenio, por lo que no puede dar una respuesta real. Sin embargo, como comentario general sobre diseño web, recomiendo el atributo "objetivo", usando cualquier valor. La 'M' en HTML es 'Marcado'; HTML no debe especificar el comportamiento, solo el significado. Para un enlace externo, recomiendo usar 'rel =" external "', y luego JavaScript para abrir dichos enlaces en una nueva ventana. [Ejemplo] (http://articles.sitepoint.com/article/standards-compliant-world/3) –

Respuesta

5

No es necesario para pasar un parámetro a SelectWindow. El navegador automáticamente le dará al foco de su nueva ventana, solo necesita decirle al selenio que ha cambiado. También asegúrese de que usted da a su nueva ventana de tiempo suficiente para cargar de hecho antes de verificar cualquier cosa:

open    /page 
click    link=Find us on Facebook! 
pause    1000 
selectWindow 
verifyTextPresent some text 
4
$this->click('css=.sf_admin_action_page:first a'); 

    $this->waitForPopUp('_blank'); 
    $this->selectWindow('_blank'); 

    $this->waitForElementPresent('css=.t-info:contains(xxx2)'); 

// ps. selenium2

1

debe usar selectPopUp para enfocar la nueva ventana. ver el documento:

selectPopUp:

  • Argumentos: WindowID - un identificador para la ventana emergente, que puede asumir un número de diferentes significados

  • simplifica el proceso de selección de una ventana emergente ventana (y no ofrece funcionalidad más allá de lo que ya se proporciona selectWindow()).

    • Si windowID no se especifica o se especifica como "nulo", se selecciona la primera ventana no superior. La ventana superior es la que seleccionaría selectWindow() sin proporcionar un ID de ventana. Esto no debería usarse cuando hay más de una ventana emergente en juego.
    • De lo contrario, la ventana se buscará considerando windowID como el siguiente en orden: 1) el "nombre" de la ventana, como se especifica a window.open(); 2) una variable de javascript que es una referencia a una ventana; y 3) el título de la ventana. Esta es la misma búsqueda ordenada realizada por selectWindow.
0

Tomé enfoque ligeramente diferente, que era forzar ningún enlace a utilizar target = _self de modo que pudieran ser probados en la misma ventana:

protected void testTextLink(WebDriver driver, final String linkText, final String targetPageTitle, final String targetPagePath) { 

    WebDriverWait wait = new WebDriverWait(driver, 20); 
    WebElement link = driver.findElement(By.linkText(linkText)); 

    // ensure that link always opens in the current window 
    JavascriptExecutor js = (JavascriptExecutor) driver; 
    js.executeScript("arguments[0].setAttribute('target', arguments[1]);", link, "_self"); 

    link.click(); 
    wait.until(ExpectedConditions.titleIs(targetPageTitle)); 

    // check the target page has the expected title 
    assertEquals(driver.getTitle(), targetPageTitle); 
    // check the target page has path 
    assertTrue(driver.getCurrentUrl().contains(targetPagePath)); 
} 
0

Simplemente utiliza este código.

public void newtab(){ 

System.setProperty("webdriver.chrome.driver", "E:\\eclipse\\chromeDriver.exe"); 

WebDriver driver = new ChromeDriver(); 

driver.get("http://www.w3schools.com/tags/att_a_target.asp"); 

//I have provided a sample link. Make sure that you have provided the correct link in the above line. 

driver.findElement(By.className("tryitbtn")).click(); 

new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")), Keys.NUMPAD2).build().perform(); 


// In keyboard we will press 

//ctrl+1 for 1st tab 

//ctrl+2 for 2nd tab 

//ctrl+3 for 3rd tab. 

//Same action is written in the above code. 

} 
//Now you can verify the text by using testNG 

Assert.assertTrue(condition); 
0

En este caso podemos utilizar KeyPress

keyPress (localizador, keySequence) Argumentos:

locator - an element locator 
    keySequence - Either be a string("\" followed by the numeric keycode of the key to be pressed, normally the ASCII value of that key), or a single character. For example: "w", "\119". [Give for CTRL+T] 

Simulates a user pressing and releasing a key. 
Cuestiones relacionadas