Web developers often need to use JavaScript pop-up dialogs to give users some information, including the following types
Read Catalogue
dialog box type
1. Warning Box: Validation results, errors or warnings that are used to prompt the user for relevant information
2. Prompt box: Used to prompt the user to enter data in the current dialog box, generally requires the user to click the Cancel or Confirm button
3. Confirm box: Used to prompt the user to confirm or cancel an action, generally require the user to click the Cancel or Confirm button
Test page
Use the following page as an example to explain, including the warning box, prompt box, confirmation box
Http://sislands.com/coin70/week1/dialogbox.htm
Selenium Code for the Action dialog box
Public Static voidTestalert (webdriver driver) {String URL= "Http://sislands.com/coin70/week1/dialogbox.htm"; Driver.get (URL); Webelement Alertbutton= Driver.findelement (By.xpath ("//input[@value = ' alert ']")); Alertbutton.click (); Alert Javascriptalert=Driver.switchto (). alert (); System.out.println (Javascriptalert.gettext ()); Javascriptalert.accept (); } Public Static voidTestprompt (Webdriver driver)throwsException {String URL= "Http://sislands.com/coin70/week1/dialogbox.htm"; Driver.get (URL); Webelement Promptbutton= Driver.findelement (By.xpath ("//input[@value = ' Prompt ']")); Promptbutton.click (); Thread.Sleep (2000); Alert javascriptprompt=Driver.switchto (). alert (); Javascriptprompt.sendkeys ("This is learning Selenium"); Javascriptprompt.accept (); System.out.println (Javascriptprompt.gettext ()); Javascriptprompt=Driver.switchto (). alert (); Javascriptprompt.accept (); Thread.Sleep (2000); Promptbutton.click (); Javascriptprompt=Driver.switchto (). alert (); Javascriptprompt.dismiss (); Thread.Sleep (2000); Javascriptprompt=Driver.switchto (). alert (); Javascriptprompt.accept (); } Public Static voidTestconfirm (Webdriver driver)throwsException {String URL= "Http://sislands.com/coin70/week1/dialogbox.htm"; Driver.get (URL); Webelement ConfirmButton= Driver.findelement (By.xpath ("//input[@value = ' Confirm ']")); Confirmbutton.click (); Thread.Sleep (2000); Alert javascriptconfirm=Driver.switchto (). alert (); Javascriptconfirm.accept (); Thread.Sleep (2000); Javascriptconfirm=Driver.switchto (). alert (); Javascriptconfirm.accept (); }
Java Selenium (11) Action Popup dialog box