forked from SeleniumHQ/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
java.html
142 lines (98 loc) · 6.04 KB
/
java.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!doctype html>
<meta charset=utf-8>
<title>The Java Support Package</title>
<link rel=stylesheet href=se.css>
<link rel=prev href=drivers.html title="Driver Idiosyncracies">
<link rel=next href=attr.html title="Copyright and Attributions">
<script src=docs.js></script>
<h1>The Java Support Package</h1>
<h2>Working With Colours</h2>
<p>You will occasionally want to validate the colour of a thing as part of your tests,
the problem is that colour definitions on the web are not constant.
Wouldn't it be nice if there was an easy way to compare
a HEX representation of a colour with an RGB representation of a colour,
or an RGBA representation of a colour with a HSLA representation of a colour?
<p>Worry not there is a solution, the <em>Color</em> class!
<p>First of all you will need to import the class:
<pre><code class=java>import org.openqa.selenium.support.Color</code></pre>
<p>You can now start creating colour objects,
every colour object will need to be created from a string representation of your colour,
supported colour representations are:
<pre><code class=java>private final Color HEX_COLOUR = Color.fromString("#2F7ED8");
private final Color RGB_COLOUR = Color.fromString("rgb(255, 255, 255)");
private final Color RGB_COLOUR = Color.fromString("rgb(40%, 20%, 40%)");
private final Color RGBA_COLOUR = Color.fromString("rgba(255, 255, 255, 0.5)");
private final Color RGBA_COLOUR = Color.fromString("rgba(40%, 20%, 40%, 0.5)");
private final Color HSL_COLOUR = Color.fromString("hsl(100, 0%, 50%)");
private final Color HSLA_COLOUR = Color.fromString("hsla(100, 0%, 50%, 0.5)");</code></pre>
<p>The Color package also supports all of the base colour definitions
specified in <a href=//www.w3.org/TR/css3-color/#html4>http://www.w3.org/TR/css3-color/#html4</a>.
<pre><code class=java>private final Color BLACK = Color.fromString("black");
private final Color CHOCOLATE = Color.fromString("chocolate");
private final Color HOTPINK = Color.fromString("hotpink");</code></pre>
<p>Sometimes browsers will return a colour value of transparent
if no colour has been set on an element,
the Color package also supports this
<pre><code class=java>private final Color TRANSPARENT = Color.fromString("transparent");</code></pre>
<p>You can now safely query an element
to get its colour/background colour knowing that
any response will be correctly parsed
and converted into a valid Color object:
<pre><code class=java>Color loginButtonColour = driver.findElement(By.id("login")).getCssValue("color");
Color loginButtonBackgroundColour = driver.findElement(By.id("login")).getCssValue("background-color");</code></pre>
<p>You can then directly compare colour objects:
<pre><code class=java>assert loginButtonBackgroundColour.equals(HOTPINK);</code></pre>
<p>Or you can convert the colour into one of the following formats
and perform a static validation:
<pre><code class=java>assert loginButtonBackgroundColour.asHex().equals("#ff69b4");
assert loginButtonBackgroundColour.asRgba().equals("rgba(255, 105, 180, 1)");
assert loginButtonBackgroundColour.asRgb().equals("rgb(255, 105, 180)");</code></pre>
<p>Colours are no longer a problem.
<h2 class=working_with_select_elements>Working With <code>select</code> Elements</h2>
<p>Select elements can require quite a bit of boiler plate code to automate.
To reduce this and make your tests cleaner there is a
<em>Select</em> class in the Selenium support package.
To use it you will need the following import:
<pre><code class=java>import org.openqa.selenium.support.ui.Select;</code></pre>
<p>You are then able to create a Select object using a WebElement that
references a <select> element.
<pre><code class=java>WebElement selectElement = driver.findElement(By.id("selectElementID"));
Select selectObject = new Select(selectElement);</code></pre>
<p>The select object will now give you a series of commands
that allow you to interact with a <select> element.
First of all you have some options to select various options
from the <select> element.
<pre><code class=html><select>
<option value=value1>Bread</option>
<option value=value2 selected>Milk</option>
<option value=value3>Cheese</option>
</select></code></pre>
<p>To select the first option from the above element
you now have three options:
<pre><code class=java>// Select an <option> based upon the <select> elements internal index
selectObject.selectByIndex(1);
// Select an <option> based upon its value attribute
selectObject.selectByValue("value1");
// Select an <option> based upon its text
selectObject.selectByVisibleText("Bread");</code></pre>
<p>You can then check which options are selected by using:
<pre><code class=java>// Return a WebElement<List> of options that have been selected
List<WebElement> allSelectedOptions = selectObject.getAllSelectedOptions();
// Return a WebElement referencing the first selection option found by walking down the DOM
WebElement firstSelectedOption = selectObject.getFirstSelectedOption();</code></pre>
<p>Or you may just be interested in what <option> elements
the <select> element contains:
<pre><code class=java>// Return a WebElement<List> of options that the <select> element contains
List<WebElement> allAvailableOptions = selectObject.getOptions();</code></pre>
<p>If you then want to deselect any elements you now have four options:
<pre><code class=java>// Deselect an <option> based upon the <select> elements internal index
selectObject.deselectByIndex(1);
// Deselect an <option> based upon its value attribute
selectObject.deselectByValue("value1");
// Deselect an <option> based upon its text
selectObject.deselectByVisibleText("Bread");
// Deselect all selected <option> elements
selectObject.deselectAll();</code></pre>
<p>Finally, some <select> elements allow you to select more than one option,
you can find out if your <select> element is one of these by using:
<pre><code class=java>Boolean doesThisAllowMultipleSelections = selectObject.isMultiple();</code></pre>