3

I'm stuck with my automation where when I enter wrong username and password it will pop up saying "Unable to login. Try different username"

def test_logonWrongUserName(self):
    self.setUpClass() # Initialize the driver 
    self.sh.navigateToUrl("http://test.com")
    self.sh.member_Login("Foo", "asd") 

Now need to test the Alert or popup (Not sure it is an alert or popup window) which has the text "Unable to login. Try different username". If it's find the text then the test pass. If not then the test has to fail.

Note:Just want to add, We can't access the html dom until we close the alert/popup window

2
  • Is this a browser popup/classic js alert? Could you show the screenshot? Thanks. Commented Jun 16, 2016 at 18:22
  • @alecxe I attached the picture. Sorry need to cross out the ip. Commented Jun 16, 2016 at 18:26

1 Answer 1

8

You need to wait for the alert to be present, assert you have the expected text inside and then close/accept the alert:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

wait = WebDriverWait(driver, 10)
wait.until(EC.alert_is_present())

alert = driver.switch_to.alert
assert "Unable to login. Try different username" in alert.text
alert.accept()
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting this error _> alert = self.sh.getDriver().switch_to.alert() TypeError: 'Alert' object is not callable
@user3174886 of course, sorry, my mistake, fixed now.
I'm going to remove the attachment. It is work related.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.