2012-11-05 9 views

risposta

1

Se non vi dà fastidio circa la posizione del testo attuale, allora si potrebbe utilizzare la proprietà Driver.PageSource come di seguito:

Driver.PageSource.Contains ("messaggio previsto");

+0

C'è un modo per trovare il testo senza l'origine della pagina e altro ho bisogno di stampare True se il testo esiste in qualsiasi punto della pagina e False se il testo non ... –

-1

Nota: Non in booleano

WebDriver driver=new FirefoxDriver(); 
driver.get("http://www.gmail.com"); 

if(driver.getPageSource().contains("Ur message")) 
    { 
    System.out.println("Pass"); 
    } 
else 
    { 
    System.out.println("Fail"); 
    } 
3

Sì, si può fare ciò che restituisce il valore booleano. Il seguente codice Java in WebDriver con TestNG o JUnit può fare:

protected boolean isTextPresent(String text){ 
    try{ 
     boolean b = driver.getPageSource().contains(text); 
     return b; 
    } 
    catch(Exception e){ 
     return false; 
    } 
    } 

Ora chiamano il metodo di cui sopra, come di seguito:

assertTrue(isTextPresent("Your text")); 

Oppure, c'è un altro modo. Penso che questo sia il modo migliore:

private StringBuffer verificationErrors = new StringBuffer(); 
try { 
        assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]* Your text here\r\n\r\n[\\s\\S]*$")); 
    } catch (Error e) { 
    verificationErrors.append(e.toString()); 
    } 
+0

La tua domanda sembra essere priva di alcune informazioni. 1) Che lingua stai usando? 2) Stai usando qualsiasi framework di test come JUnit o TestNG? –

+0

Sì, preferisco l'ultima risposta sopra –

+0

Questa è una soluzione migliore in quanto può essere sfruttata per più valori di testo –

1

Driver.getPageSource() è un cattivo modo per verificare il testo presente. Supponiamo che tu dica, driver.getPageSource().contains("input"); Che non verifica che "input" sia presente sullo schermo, solo che "input" è presente nell'html, come un tag di input.

Io di solito a verificare il testo su un elemento utilizzando XPath:

boolean textFound = false; 
try { 
    driver.findElement(By.xpath("//*[contains(text(),'someText')]")); 
    textFound = true; 
} catch (Exception e) { 
    textFound = false; 
} 

Se volete delle partite testo esatto, è sufficiente rimuovere la funzione contiene:

driver.findElement(By.xpath("//*[text()='someText'])); 
5

Come zmorris punti driver.getPageSource().contains("input"); non è il soluzione adeguata perché cerca in tutti gli html, non solo i testi su di esso. vi suggerisco di controllare questa domanda: how can I check if some text exist or not in the page? e il modo in Recomended spiegato dal Slanec:

String bodyText = driver.findElement(By.tagName("body")).getText(); 
Assert.assertTrue("Text not found!", bodyText.contains(text)); 
+0

Non dovrebbe essere: Stringa bodyText = driver.findElement (By.tagName ("body")) .getText(); Assert.assertTrue (true, bodyText.contains (text)); – Cagy79

+0

Questo non funziona in Edge – SOAlgorithm

0

Per programmatori Ruby ecco come si può affermare. devono includere Minitest per ottenere il asserisce

assert(@driver.find_element(:tag_name => "body").text.include?("Name")) 
0

Di seguito il codice è il modo più adatto per verificare un testo sulla pagina. Puoi utilizzare uno qualsiasi degli 8 locatori secondo la tua convenienza.

String Verifytext = driver.findElement (By.tagName ("body")). GetText(). Trim(); Assert.assertEquals (Verifytext, "Incolla qui il testo che deve essere verificato");

0

Se si desidera controllare visualizzati solo gli oggetti (C#):

public bool TextPresent(string text, int expectedNumberOfOccurrences) 
    { 
     var elements = Driver.FindElements(By.XPath(".//*[text()[contains(.,'" + text + "')]]")); 
     var dispayedElements = 0; 
     foreach (var webElement in elements) 
     { 
      if (webElement.Displayed) 
      { 
       dispayedElements++; 
      } 
     } 
     var allExpectedElementsDisplayed = dispayedElements == expectedNumberOfOccurrences; 
     return allExpectedElementsDisplayed; 
    } 
Problemi correlati