The core libraries of Selenium try to be low level and non-opinionated. The Support classes in each language provide opinionated wrappers for common interactions that may be used to simplify some behaviors.
これは、このセクションの複数ページの印刷可能なビューです。 印刷するには、ここをクリックしてください.
Support features
1 - Waiting with Expected Conditions
Expected Conditions are used with Explicit Waits. Instead of defining the block of code to be executed with a lambda, an expected conditions method can be created to represent common things that get waited on. Some methods take locators as arguments, others take elements as arguments.
These methods can include conditions such as:
- element exists
- element is stale
- element is visible
- text is visible
- title contains specified value
2 - Command Listeners
These allow you to execute custom actions in every time specific Selenium commands are sent
3 - 色を扱う
テストの一部として何かの色を検証したい場合があります。 問題は、ウェブ上の色の定義が一定ではないことです。 色のHEX表現を色のRGB表現と比較する簡単な方法、または色のRGBA表現を色のHSLA表現と比較する簡単な方法があったらいいのではないでしょうか?
心配しないでください。解決策があります。: Color クラスです!
まず、クラスをインポートする必要があります。
これで、カラーオブジェクトの作成を開始できます。 すべての色オブジェクトは、色の文字列表現から作成する必要があります。 サポートされている色表現は、以下のとおりです。
Colorクラスは、 http://www.w3.org/TR/css3-color/#html4 で指定されているすべての基本色定義もサポートしています。
要素に色が設定されていない場合、ブラウザは “透明” の色の値を返すことがあります。 Colorクラスもこれをサポートしています。
レスポンスが正しく解析され、有効なColorオブジェクトに変換されることを認識して、要素を安全にクエリしてその色/背景色を取得できるようになりました。
そして、色オブジェクトを直接比較できます。
または、色を次の形式のいずれかに変換し、静的に検証することができます。
色はもはや問題ではありません。
4 - 選択要素の操作
The Select object will now give you a series of commands
that allow you to interact with a <select>
element.
If you are using Java or .NET make sure that you’ve properly required the support package in your code. See the full code from GitHub in any of the examples below.
Note that this class only works for HTML elements select
and option
.
It is possible to design drop-downs with JavaScript overlays using div
or li
,
and this class will not work for those.
Types
Select methods may behave differently depending on which type of <select>
element is being worked with.
Single select
This is the standard drop-down object where one and only one option may be selected.
Multiple select
This select list allows selecting and deselecting more than one option at a time.
This only applies to <select>
elements with the multiple
attribute.
Create class
First locate a <select>
element, then use it to initialize a Select
object.
Note that as of Selenium 4.5, you can’t create a Select
object if the <select>
element is disabled.
List options
There are two lists that can be obtained:
All options
Get a list of all options in the <select>
element:
Selected options
Get a list of selected options in the <select>
element. For a standard select list
this will only be a list with one element, for a multiple select list it can contain
zero or many elements.
Select option
The Select class provides three ways to select an option. Note that for multiple select type Select lists, you can repeat these methods for each element you want to select.
Text
Select the option based on its visible text
Value
Select the option based on its value attribute
Index
Select the option based on its position in the list
Disabled options
Options with a disabled
attribute may not be selected.
De-select option
Only multiple select type select lists can have options de-selected. You can repeat these methods for each element you want to select.
5 - ThreadGuard
このクラスは、Javaバインディングでのみ使用可能です。
ThreadGuardは、ドライバーが、それを作成した同じスレッドからのみ呼び出されることを確認します。 特に並行してテストを実行する場合のスレッドの問題は、不可解でエラーの診断が難しい場合があります。 このラッパーを使用すると、このカテゴリのエラーが防止され、発生時に例外が発生します。
次の例は、スレッドの衝突をシミュレートします。
結果は以下のとおりです。
下記例を参照してください。
protectedDriver
はメインスレッドで作成されます- Java
Runnable
を使用して新しいプロセスを起動し、新しいスレッド
を使用してプロセスを実行します - メインスレッドのメモリに
protectedDriver
がないため、両方のスレッド
が衝突します。 ThreadGuard.protect
は例外をスローします。
注意:
これは、並列実行時にドライバーを管理するために ThreadLocal
を使用する必要性を置き換えるものではありません。