Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python / Selenium : Switch to an alert and verify the text within

My issue: Being able to detect an alert box has been opened, verify the text inside the alert, confirm the text and then close the alert. Then, go back and correct the invalid email.

My test is one for registering a user. I need to validate when someone might input incorrect data or mismatched data. In this case, I am entering an email and verifying the email first entered. Should they have a mismatch, a popup will appear alerting the user to check the email addresses they have entered and that they match. So far all I can get is an error.

Error:

E UnexpectedAlertPresentException: Message: u'Modal dialog present' ; Stacktrace: Method nsCommandProcessor.prototype.execute threw an error in file:///var/folders/1j/x3cxkrqn3sscdyq0t36169hr0000gn/T/tmpTCqMgm/extensions/[email protected]/components/command_processor.js

I thought my code would handle this, but nope. If someone could point out my, obvious to you, mistake I would be grateful.

My entire test code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class ChallengeTests(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(5)
        self.base_url = "https://www.testpage.com"
        self.verificationErrors = []
        self.accept_next_alert = True

# SIGN UP NEW USER

    def test_00_sign_up(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("remail").send_keys("[email protected]")
        driver.find_element_by_id("remail_confirm").send_keys("[email protected]")
        driver.find_element_by_id("next").click()
        alert = self.driver.switch_to_alert()
        alert = self.assertTrue(self.is_text_present("The email addresses you entered"))
        driver.find_element_by_id("remail_confirm").send_keys("[email protected]")
        driver.find_element_by_id("registration_button").click()

# NON TESTS

    def is_element_present(self, how, what):
        try:
            self.driver.find_element(by=how, value=what) # to find page elements
        except NoSuchElementException, e: return False
        return True     

    def is_text_present(self, text):
        try:
            body = self.driver.find_element_by_tag_name("body") # find body tag element
        except NoSuchElementException, e: return False
        return text in body.text # check if the text is in body's text

    def is_alert_present(self):
        try:
            self.driver.switch_to_alert()
        except NoAlertPresentException, e: 
            return False

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True        
        return True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)
like image 384
Dave Avatar asked Aug 28 '13 00:08

Dave


2 Answers

On newer version of Python(3.4)

def is_alert_present(self):
    try:
        self.driver.switch_to_alert()
    except NoAlertPresentException: 
        return False
like image 80
user3174886 Avatar answered Sep 28 '22 04:09

user3174886


Try waiting for the alert to appear, before directly switching to it.

If that does not work, I have a feeling that the popup actually is a weblement and not a JS alert. if that is the case try looking for a selector using the browser developer tools and directly interact with it.

like image 42
Amey Avatar answered Sep 28 '22 04:09

Amey