How to capture the alert message using the web browser controler?

4

I'm developing a robot using Asp.Net C # web browser controler.

In this robot I need to identify some messages that appear in Javascript Alert.

The site that Robot is manipulating is not mine, so there's no way I can make any changes in the structure of it.

So, do you have any way to get Alert content?

    
asked by anonymous 09.07.2015 / 15:08

2 answers

3

Perhaps the best alternative for you is to use Selenium Web Driver

With it you can manipulate your browser and have access to all elements of the DOM, allowing you to simulate a user browsing the site, and you can also execute JavaSCript commands, access CSS, etc.

Below is a basic example of how to get the text of an alert using the Selenium Webdriver:

using NUnit.Framework;
using OpenQA.Selenium.Firefox;

namespace ExemploSeleniumWebDriver
{
    // A classe robo deve ser uma classe de teste para ser executado automaticamente.
    [TestFixture]
    public class Robo
    {
        // As ações do robô são implementadas como testes unitários.
        [Test]
        public void LeiaMensagemDeAlert()
        {
            // Usei aqui o driver do Firefox, mas poderia ser de qualquer outro browser.
            FirefoxDriver driver = new FirefoxDriver();

            // Navega para a página que criei para simular uma mensagem de alert.
            driver.Navigate().GoToUrl("TesteSeleniumWebDriver.html");

            // Clica no botão.
            driver.FindElementByName("botaoDeAlerta").Click();

            // Obtém o texto do Alert.
            string textoDoAlert = driver.SwitchTo().Alert().Text;

            // Fecha a janela do alert.
            driver.SwitchTo().Alert().Dismiss();

            // Digita a mensagem do alert no campo de texto da tela.
            driver.FindElementById("campoDeTexto").SendKeys("O alert continha o seguinte texto: " + textoDoAlert);
        }
    }
}

Content of the TestSeleniumWebDriver.html file:

<html>
<head>
    <title>Exemplo do Selenium Web Driver</title>
</head>
<body>
    <input name="botaoDeAlerta" type="button" value="Mostrar Alert" onclick="alert('Teste de obtenção da mensagem de alerta via C#.')" />

    <br />

    <label>Selenium WebDriver irá preencher essa caixa de texto com o texto do alerta:</label>
    <br />
    <input type="text" id="campoDeTexto" />
</body>
</html>
    
17.07.2015 / 15:32
2

To get the message you will have to do the following:

1 - Insert a script tag into the head of your webbrowser containing a new implementation of the window.alert function, this implementation should add the alert string to some element of the html.

2 - Perform the procedure so that the alert is displayed and consequently the element with the alert string is popular.

3- Retrieve the string placed on this element

First Step

Create tag script to be inserted

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement tagScript = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement bloqAlert = (IHTMLScriptElement)tagScript.DomElement;

Now we will define the string representing the contents of the script tag:

You should implement the alert code here, knowing that you will receive the alert message as an argument

bloqAlert.text = "window.alert = function (msgAlert) {
//SE A PÁGINA UTILIZAR JQUERY UM EXEMPLO SERIA
$('#elementoUsadoParaReceberAMensagem').val(msgAlert);
}";

Second Step

Adds the created script tag to the head of the loaded document in your webbrowser

head.AppendChild(scriptEl);

If the alert is invoked on the page your message will go to this html element

Third Step

Retrieves the string placed in the element

string mensagemAlert = webBrowser1.Document.GetElementById("elementoUsadoParaReceberAMensagem").GetAttribute("value");
    
19.07.2015 / 11:21