JQuery UI uses jQuery UI to implement dialog boxes and pop -up frames in the Java class library
JQuery UI uses jQuery UI to implement dialog boxes and pop -up frames in the Java class library
In the Java application, the use of the jQuery UI library can easily implement the function of the dialog box and the pop -up box.The dialog box and pop -up box can be used to display important information, receive user input or confirmation.This tutorial will introduce these functions how to implement these functions through jQuery UI in the Java library and provide some useful skills.
1. Introduce jQuery UI library
First, you need to introduce the jQuery UI library in the Java project.It can be achieved by adding the following dependencies in the project:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery-ui</artifactId>
<version>1.12.1</version>
</dependency>
In this way, you can use the dialog box and pop -up box assembly in the JQURY UI library in the Java class.
2. Create a dialog box
Below is a simple example, showing how to use jquery UI in the Java class library to create a basic dialog box:
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class DialogExample {
public static void main(String[] args) {
// Create the Firefox driver
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
FirefoxOptions options = new FirefoxOptions();
options.addArguments("--headless");
WebDriver driver = new FirefoxDriver(options);
// Load the jQuery UI library and related scripts
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.onload = function() { " +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
"script.src = 'https://code.jquery.com/ui/1.12.1/jquery-ui.js';" +
"document.head.appendChild(script);" +
"}");
// Create a dialog box
js.executeScript("$(function() { " +
"$('<div>').attr('id', 'dialog').appendTo('body');" +
"}");
// Display dialog boxes
js.executeScript("$(function() { " +
"$('#dialog').dialog({ " +
"Title: 'Dialog box title'," +
"modal: true, " +
"buttons: { " +
"OK: Function () {" +
"$(this).dialog('close'); " +
"} " +
"} " +
"}); " +
"}");
}
}
In this example, we use the JavaScripxecutor` interface to execute the JavaScript script.First, we load the jQuery UI library and related scripts.Then, we create a dialog box with a title and a certain button and show it.
3. Create a pop -up box
JQuery UI in the Java library can also be used to create a pop -up box.The following is an example:
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class PopupExample {
public static void main(String[] args) {
// Create the Chrome Driver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
// Load the jQuery UI library and related scripts
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.onload = function() { " +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
"script.src = 'https://code.jquery.com/ui/1.12.1/jquery-ui.js';" +
"document.head.appendChild(script);" +
"}");
// Create a pop -up box
js.executeScript("$(function() { " +
"$ ('<div>'). Attr ('ID', 'Popup'). html ('This is a pop -up box') .appndto ('body');" +
"}");
// Show the pop -up box
js.executeScript("$(function() { " +
"$('#popup').dialog({ " +
"Title: 'Popping box title'," +
"modal: true, " +
"buttons: { " +
"OK: Function () {" +
"$(this).dialog('close'); " +
"} " +
"} " +
"}); " +
"}");
}
}
In this example, we used the `ChristEdriver` and` Chromeoptions "classes to create the Chrome driver.Then, we loaded the jQuery UI library and related scripts, and created a pop -up box containing text content.Finally, we display the pop -up box.
Fourth, skills and precautions
-When the creation dialog box and the pop -up box, make sure that the jQuery UI library and related scripts have been correctly loaded.
-The style and layout of the dialog box and the pop -up box can be customized as needed.
-It different configuration options are based on the definition dialog box and pop -up box, such as title, width, height, whether it can be dragged, etc.
-Ad the click event of the button and define the button of the button.
-It can display complex content in the dialog box and pop -up box, such as HTML, table, etc.
Through this tutorial, you have learned how to use the jQuery UI in the Java class library to implement the dialog box and the function of the pop -up box.You can expand and customize according to your needs to meet the requirements of the project.