-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathassert-values-of-attributes.js
66 lines (58 loc) · 2.06 KB
/
assert-values-of-attributes.js
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
/**
* Usage:
* In the case when you want to assert a value of a specified attribute.
*
* Change the values written below.
* selector: A string of a selector to pinpoint the element.
* attrName: A string of the attribute which you want to assert its value.
* expected: A string of the your expected value.
* errMessage:
* A string of the error message which you want to show when it fails.
* It will be a default string if you don't change from `<TODO: REPLACE>`.
*
* 使い方:
* 特定の属性の値を検証したいときにご利用ください。
*
* 以下の値を変更してください
* selector: 対象要素を特定するセレクタの文字列
* attrName: 値を検証したい属性の文字列
* expected: 検証対象属性に期待する値の文字列
* errMessage:
* 検証が失敗したときに表示したいエラーメッセージの文字列
* `<TODO: REPLACE>` のまま変更がない場合はデフォルトの文言で表示します。
*/
var selector = "<TODO: REPLACE>";
var attrName = "<TODO: REPLACE>"; /* e.g) id, class, data */
var expected = "<TODO: REPLACE>";
var errMessage = "<TODO: REPLACE>";
/**
* --------- You don't need to touch below ---------------
* --------- ここから下は変える必要はありません ----------
*/
/**
* Locate the element
* 要素を探す
*/
var element = document.querySelector(selector);
/**
* Stop process if it does not exist
* 要素がなければ処理を中断する
*/
if (!element) {
throw new Error('Error: cannot find the element with selector(' + selector + ').');
}
/**
* Get value from the elment
* 属性の値を取得する
*/
var actual = element.getAttribute(attrName);
/**
* Stop process if actual and expected are different
* 実際のものと期待するものが違う場合処理を中断する
*/
if (actual !== expected) {
if (errMessage == "<TODO: REPLACE>") {
errMessage = 'Error: expected(' + expected + ') and actual(' + actual + ') are not same.';
}
throw new Error(errMessage);
}