|
1 | 1 | --- |
2 | | -title: "Customizing a Node" |
3 | | -linkTitle: "Customize Node" |
| 2 | +title: "自定义Node" |
| 3 | +linkTitle: "自定义Node" |
4 | 4 | weight: 4 |
5 | 5 | --- |
6 | 6 |
|
7 | | -{{% pageinfo color="warning" %}} |
8 | | -<p class="lead"> |
9 | | - <i class="fas fa-language d-4"></i> |
10 | | - Page being translated from |
11 | | - English to Chinese. Do you speak Chinese? Help us to translate |
12 | | - it by sending us pull requests! |
13 | | -</p> |
14 | | -{{% /pageinfo %}} |
15 | 7 |
|
16 | | -## How to customize a Node |
| 8 | +## 如何自定义节点 |
17 | 9 |
|
18 | | -There are times when we would like a Node to be customized to our needs. |
| 10 | +有时候我们希望根据自己的需求自定义Node。 |
19 | 11 |
|
20 | | -For e.g., we may like to do some additional setup before a session begins execution and some clean-up after a session runs to completion. |
| 12 | +例如,我们可能希望在会话开始执行前做一些额外的设置,在会话完成后做一些清理工作。 |
21 | 13 |
|
22 | | -Following steps can be followed for this: |
| 14 | +可以按照以下步骤进行: |
23 | 15 |
|
24 | | -* Create a class that extends `org.openqa.selenium.grid.node.Node` |
25 | | -* Add a static method (this will be our factory method) to the newly created class whose signature looks like this: |
| 16 | +* 创建一个类,继承自 `org.openqa.selenium.grid.node.Node` |
| 17 | +* 在新建的类中添加一个静态方法(工厂方法),其签名如下: |
26 | 18 |
|
27 | | - `public static Node create(Config config)`. Here: |
| 19 | + `public static Node create(Config config)`。其中: |
28 | 20 |
|
29 | | - * `Node` is of type `org.openqa.selenium.grid.node.Node` |
30 | | - * `Config` is of type `org.openqa.selenium.grid.config.Config` |
31 | | -* Within this factory method, include logic for creating your new Class. |
32 | | -* To wire in this new customized logic into the hub, start the node and pass in the fully qualified class name of the above class to the argument `--node-implementation` |
| 21 | + * `Node` 类型为 `org.openqa.selenium.grid.node.Node` |
| 22 | + * `Config` 类型为 `org.openqa.selenium.grid.config.Config` |
| 23 | +* 在工厂方法中,包含创建新类的逻辑。 |
| 24 | +* 要将自定义逻辑接入 hub,启动 node 时通过参数 `--node-implementation` 传入上述类的完整类名。 |
33 | 25 |
|
34 | | -Let's see an example of all this: |
| 26 | +下面我们来看一个完整的示例: |
35 | 27 |
|
36 | | -### Custom Node as an uber jar |
| 28 | +### 作为 uber jar 的自定义 Node |
37 | 29 |
|
38 | | -1. Create a sample project using your favourite build tool (**Maven**|**Gradle**). |
39 | | -2. Add the below dependency to your sample project. |
| 30 | +1. 使用你喜欢的构建工具 (**Maven**|**Gradle**) 创建一个示例项目。 |
| 31 | +2. 在项目中添加如下依赖: |
40 | 32 | * [org.seleniumhq.selenium/selenium-grid](https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-grid) |
41 | | -3. Add your customized Node to the project. |
42 | | -4. Build an [uber jar](https://imagej.net/develop/uber-jars) to be able to start the Node using `java -jar` command. |
43 | | -5. Now start the Node using the command: |
| 33 | +3. 将你的自定义 Node 添加到项目中。 |
| 34 | +4. 构建一个 [uber jar](https://imagej.net/develop/uber-jars),以便通过 `java -jar` 命令启动 Node。 |
| 35 | +5. 现在使用如下命令启动 Node: |
44 | 36 |
|
45 | 37 | ```bash |
46 | 38 | java -jar custom_node-server.jar node \ |
47 | 39 | --node-implementation org.seleniumhq.samples.DecoratedLoggingNode |
48 | 40 | ``` |
49 | 41 |
|
50 | | -**Note:** If you are using Maven as a build tool, please prefer using [maven-shade-plugin](https://maven.apache.org/plugins/maven-shade-plugin) instead of [maven-assembly-plugin](https://maven.apache.org/plugins/maven-assembly-plugin) because maven-assembly plugin seems to have issues with being able to merge multiple Service Provider Interface files (`META-INF/services`) |
| 42 | +**注意:** 如果你使用 Maven 作为构建工具, |
| 43 | +建议使用 [maven-shade-plugin](https://maven.apache.org/plugins/maven-shade-plugin) |
| 44 | +而不是 [maven-assembly-plugin](https://maven.apache.org/plugins/maven-assembly-plugin), |
| 45 | +因为 maven-assembly-plugin 在合并多个 Service Provider Interface 文件 (`META-INF/services`) |
| 46 | +时可能存在问题。 |
51 | 47 |
|
52 | | -### Custom Node as a regular jar |
| 48 | +### 作为普通 jar 的自定义 Node |
53 | 49 |
|
54 | | -1. Create a sample project using your favourite build tool (**Maven**|**Gradle**). |
55 | | -2. Add the below dependency to your sample project. |
| 50 | +1. 使用你喜欢的构建工具 (**Maven**|**Gradle**) 创建一个示例项目。 |
| 51 | +2. 在项目中添加如下依赖: |
56 | 52 | * [org.seleniumhq.selenium/selenium-grid](https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-grid) |
57 | | -3. Add your customized Node to the project. |
58 | | -4. Build a jar of your project using your build tool. |
59 | | -5. Now start the Node using the command: |
| 53 | +3. 将你的自定义 Node 添加到项目中。 |
| 54 | +4. 使用构建工具构建项目 jar 包。 |
| 55 | +5. 现在使用如下命令启动 Node: |
60 | 56 |
|
61 | 57 | ```bash |
62 | 58 | java -jar selenium-server-4.6.0.jar \ |
63 | 59 | --ext custom_node-1.0-SNAPSHOT.jar node \ |
64 | 60 | --node-implementation org.seleniumhq.samples.DecoratedLoggingNode |
65 | 61 | ``` |
66 | | -Below is a sample that just prints some messages on to the console whenever there's an activity of interest (session created, session deleted, a webdriver command executed etc.,) on the Node. |
67 | | - |
| 62 | +下面是一个示例:当Node有相关活动(会话创建、会话删除、WebDriver 命令执行等)时,仅在控制台打印一些消息。 |
68 | 63 |
|
69 | 64 | <details> |
70 | | -<summary>Sample customized node</summary> |
| 65 | +<summary>自定义Node示例</summary> |
71 | 66 |
|
72 | 67 | ```java |
73 | 68 | package org.seleniumhq.samples; |
@@ -239,21 +234,21 @@ public class DecoratedLoggingNode extends Node { |
239 | 234 | ``` |
240 | 235 | </details> |
241 | 236 |
|
242 | | -**_Foot Notes:_** |
| 237 | +**_注释:_** |
243 | 238 |
|
244 | | -In the above example, the line `Node node = LocalNodeFactory.create(config);` explicitly creates a `LocalNode`. |
| 239 | +在上述示例中,`Node node = LocalNodeFactory.create(config);` 这一行显式创建了一个 `LocalNode`。 |
245 | 240 |
|
246 | | -There are basically 2 types of *user facing implementations* of `org.openqa.selenium.grid.node.Node` available. |
| 241 | +`org.openqa.selenium.grid.node.Node` 主要有两种*面向用户的实现*。 |
247 | 242 |
|
248 | | -These classes are good starting points to learn how to build a custom Node and also to learn the internals of a Node. |
| 243 | +这些类是学习如何构建自定义 Node 以及了解 Node 内部机制的良好起点。 |
249 | 244 |
|
250 | | -* `org.openqa.selenium.grid.node.local.LocalNode` - Used to represent a long running Node and is the default implementation that gets wired in when you start a `node`. |
251 | | - * It can be created by calling `LocalNodeFactory.create(config);`, where: |
252 | | - * `LocalNodeFactory` belongs to `org.openqa.selenium.grid.node.local` |
253 | | - * `Config` belongs to `org.openqa.selenium.grid.config` |
254 | | -* `org.openqa.selenium.grid.node.k8s.OneShotNode` - This is a special reference implementation wherein the Node gracefully shuts itself down after servicing one test session. This class is currently not available as part of any pre-built maven artifact. |
255 | | - * You can refer to the source code [here](https://github.com/SeleniumHQ/selenium/blob/trunk/java/src/org/openqa/selenium/grid/node/k8s/OneShotNode.java) to understand its internals. |
256 | | - * To build it locally refer [here](https://github.com/SeleniumHQ/selenium/blob/trunk/deploys/k8s/README.md). |
257 | | - * It can be created by calling `OneShotNode.create(config)`, where: |
258 | | - * `OneShotNode` belongs to `org.openqa.selenium.grid.node.k8s` |
259 | | - * `Config` belongs to `org.openqa.selenium.grid.config` |
| 245 | +* `org.openqa.selenium.grid.node.local.LocalNode` - 用于表示长时间运行的 Node,也是默认实现。启动 `node` 时会自动接入。 |
| 246 | + * 可通过 `LocalNodeFactory.create(config);` 创建,其中: |
| 247 | + * `LocalNodeFactory` 属于 `org.openqa.selenium.grid.node.local` |
| 248 | + * `Config` 属于 `org.openqa.selenium.grid.config` |
| 249 | +* `org.openqa.selenium.grid.node.k8s.OneShotNode` - 这是一个特殊的参考实现,节点在服务完一个测试会话后会自动关闭。该类目前未包含在任何预构建的 maven 包中。 |
| 250 | + * 你可以在 [这里](https://github.com/SeleniumHQ/selenium/blob/trunk/java/src/org/openqa/selenium/grid/node/k8s/OneShotNode.java) 查看源码。 |
| 251 | + * 参考 [这里](https://github.com/SeleniumHQ/selenium/blob/trunk/deploys/k8s/README.md) 了解本地构建方法。 |
| 252 | + * 可通过 `OneShotNode.create(config)` 创建,其中: |
| 253 | + * `OneShotNode` 属于 `org.openqa.selenium.grid.node.k8s` |
| 254 | + * `Config` 属于 `org.openqa.selenium.grid.config` |
0 commit comments