2010-07-19 3 views
6

Alcuni link sulla nostra pagina si aprono in una nuova finestra usando target = "_ blank". Come posso fare in modo che il selenio guardi nella finestra giusta per verificare che la pagina si colleghi alla pagina giusta?Come verificare un target = "_ blank" link usando selenio?

Ecco quello che ho provato:

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

non ho familiarità con selenio, quindi non posso eventualmente dare una risposta vera e propria. Tuttavia, come commento generale sul web design, raccomando contro l'attributo 'target', usando qualsiasi valore. La 'M' in HTML è 'Markup'; L'HTML non dovrebbe specificare il comportamento, solo il significato. Per un collegamento esterno, ti consiglio di utilizzare 'rel =" external "', quindi JavaScript per rendere tali collegamenti aperti in una nuova finestra. [Esempio] (http://articles.sitepoint.com/article/standards-compliant-world/3) –

risposta

5

Non è necessario passare un parametro a selectWindow. Il browser fornirà automaticamente la nuova finestra, è sufficiente dire al selenio che è cambiato. Assicurarsi inoltre di dare il vostro nuova finestra abbastanza tempo per caricare in realtà prima di verificare qualsiasi 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

si dovrebbe usare selectPopUp per mettere a fuoco la nuova finestra. vedere il suo documento:

selectPopUp:

  • Argomenti: WindowID - un identificatore per la finestra pop-up, che può assumere un certo numero di diversi significati

  • semplifica il processo di selezione di un pop-up finestra (e non offre funzionalità al di là di quanto selectWindow() già fornisce).

    • Se windowID non è specificato o specificato come "null", viene selezionata la prima finestra non superiore. La finestra superiore è quella che verrebbe selezionata da selectWindow() senza fornire un windowID. Questo non dovrebbe essere usato quando più di una finestra popup è in gioco.
    • In caso contrario, la finestra verrà esaminata considerando windowID come segue nell'ordine: 1) il "nome" della finestra, come specificato in window.open(); 2) una variabile javascript che è un riferimento a una finestra; e 3) il titolo della finestra. Questa è la stessa ricerca ordinata eseguita da selectWindow.
0

ho preso approccio leggermente diverso, che era quello di costringere tutti i link per utilizzare target = _self in modo che potessero essere testati nella stessa finestra:

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

semplicemente utilizzare questo codice.

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

In questo caso possiamo usare KeyPress

keyPress (locator, keySequence) Argomenti:

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.