Selenium modal processing dialog box

Source: Internet
Author: User

Address: http://www.myexception.cn/open-source/969585.html


Selenium currently does not provide processing for the IE Modal Dialog Box (that is, the pop-up dialog box opened through the showmodaldialog method. The reason is that the modal dialog box suspends the Js of the parent page until the processing of the dialog box is complete. Because the underlying implementation of selenium is based on JS, the modal dialog box will suspend selenium at the same time. Selenium cannot select the modal dialog box until it times out.

However, many systems often have a large number of Modal Dialog Box applications. These applications can be divided into two categories: select a user, select a business type, and other operations. These operations aim to fill the corresponding fields on the parent page without triggering other operations. The other type is special, for example, for workflow distribution, this type of operation will continue to execute a series of operations after selecting the return value in the modal dialog box (that is, the call of the showmodaldialog method is stuck in the middle of a bunch of JS operations ).

In the first case, you can create corresponding data constants in the test code and avoid calling the showmodaldialog method by directly assigning values;

In the second case, you can only use hack.


In the current test case, use the following hack method:
A typical modal dialog box is displayed after you click a page element (such as a button). When you click the page element for the first time, we overwrite the showmodaldialog method and change the modal dialog box to window. open the web page and select selenium from the pop-up page:

Java code
Public void clickandselectmodaldialog (string Locator ){
Clickformodaldialog (Locator );
Selenium. selectwindow ("name = modal ");
}

Private void clickformodaldialog (string Locator ){
String overrideshowmodaldialogjs = "If (selenium. browserbot. getcurrentwindow (). showmodaldialog ){";

Overrideshowmodaldialogjs + = "Selenium. browserbot. getcurrentwindow (). showmodaldialog = function (Surl, varguments, sfeatures )";
 
Overrideshowmodaldialogjs + = "Selenium. browserbot. getcurrentwindow (). Open (Surl, 'modal', sfeatures );";

Overrideshowmodaldialogjs ++ = "};}";
// Overwrite the showmodaldialog Method
Selenium. geteval (overrideshowmodaldialogjs );
Selenium. Click (Locator );
Selenium. OpenWindow ("", "Modal ");
Selenium. waitforpopup ("modal", "15000 ");
 
}
Public void clickandselectmodaldialog (string Locator ){
Clickformodaldialog (Locator );
Selenium. selectwindow ("name = modal ");
}

Private void clickformodaldialog (string Locator ){
String overrideshowmodaldialogjs = "If (selenium. browserbot. getcurrentwindow (). showmodaldialog ){";

Overrideshowmodaldialogjs + = "Selenium. browserbot. getcurrentwindow (). showmodaldialog = function (Surl, varguments, sfeatures )";

Overrideshowmodaldialogjs + = "Selenium. browserbot. getcurrentwindow (). Open (Surl, 'modal', sfeatures );";

Overrideshowmodaldialogjs ++ = "};}";
// Overwrite the showmodaldialog Method
Selenium. geteval (overrideshowmodaldialogjs );
Selenium. Click (Locator );
Selenium. OpenWindow ("", "Modal ");
Selenium. waitforpopup ("modal", "15000 ");

}


Next, you can perform operations on the pop-up page to obtain the values to be returned to the parent page. These values are generally an array. After obtaining the value, close the pop-up page and return to the parent page. On the parent page, click the element in the open modal dialog box again to overwrite the showmodaldialog method for the second time and return the value obtained above.

Java code
Public void acceptmodalvalue (string locator, string [] values ){
String overrideshowmodaldialogjs = "If (selenium. browserbot. getcurrentwindow (). showmodaldialog ){";

Overrideshowmodaldialogjs + = "Selenium. browserbot. getcurrentwindow (). showmodaldialog = function (Surl, varguments, sfeatures )";
 
Overrideshowmodaldialogjs + = "{" + generatemodaldialogreturnobject (values) + "Return Temp ";
 
Overrideshowmodaldialogjs ++ = "};}";
// Overwrite the showmodaldialog Method
Selenium. geteval (overrideshowmodaldialogjs );
Selenium. Click (Locator );
}

Private void generatemodaldialogreturnobject (string [] values ){
Stringbuffer returnobject = new stringbuffer ();
Returnobject. append ("Var temp = new array ();");
For (INT I = 0; I <values. length; I ++ ){
Returnobject. append ("Temp [" + I + "] = '" + values [I] + "';");
}
Return returnobject. tostring ();
}
Public void acceptmodalvalue (string locator, string [] values ){
String overrideshowmodaldialogjs = "If (selenium. browserbot. getcurrentwindow (). showmodaldialog ){";

Overrideshowmodaldialogjs + = "Selenium. browserbot. getcurrentwindow (). showmodaldialog = function (Surl, varguments, sfeatures )";

Overrideshowmodaldialogjs + = "{" + generatemodaldialogreturnobject (values) + "Return Temp ";

Overrideshowmodaldialogjs ++ = "};}";
// Overwrite the showmodaldialog Method
Selenium. geteval (overrideshowmodaldialogjs );
Selenium. Click (Locator );
}

Private void generatemodaldialogreturnobject (string [] values ){
Stringbuffer returnobject = new stringbuffer ();
Returnobject. append ("Var temp = new array ();");
For (INT I = 0; I <values. length; I ++ ){
Returnobject. append ("Temp [" + I + "] = '" + values [I] + "';");
}
Return returnobject. tostring ();
}


This completes the entire process. Let's look at an example. This example opens a department selection modal dialog box, selects a department from the department tree, and then returns:

Java code
// The Department selection box is displayed.
String depchooselocator = "... /IMG ";
<Span style = "color: #000000"> // click and select the pop-up page.
Clickandselectmodaldialog (depchooselocator); </span>
// Perform the operations on the department tree

// Obtain the value that the dialog box returns to the parent page.
String name = Selenium. geteval ("window. seltree. getallchecktext ();");
String id = Selenium. geteval ("window. seltree. getallnodeid ();");
<Span style = "color: #000000"> // closes the Department selection dialog box.
Closemodaldialog (); </span>
// Return to the parent page
Selenium. selectwindow ("name = main95598 ");
// Assemble the return value
String returnvalues = new string [2];
Returnvalues [0] = Name;
Returnvalues [1] = ID;
<Span style = "color: #000000"> // obtain the return value of the dialog box on the parent page
Acceptmodalvalue (depchooselocator, returnvalues); </span>
// The Department selection box is displayed.
String depchooselocator = "... /IMG ";
// Click and select the pop-up page
Clickandselectmodaldialog (depchooselocator );
// Perform the operations on the department tree
....
// Obtain the value that the dialog box returns to the parent page.
String name = Selenium. geteval ("window. seltree. getallchecktext ();");
String id = Selenium. geteval ("window. seltree. getallnodeid ();");
// Close the Department selection dialog box
Closemodaldialog ();
// Return to the parent page
Selenium. selectwindow ("name = main95598 ");
// Assemble the return value
String returnvalues = new string [2];
Returnvalues [0] = Name;
Returnvalues [1] = ID;
// Obtain the return value of the dialog box on the parent page
Acceptmodalvalue (depchooselocator, returnvalues );


It can be further abstracted as the template callback method, omitted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.