|
1 |
| -<h2>Проверка элемента на странице</h2> |
| 1 | +<h2>Checking an element on a page</h2> |
2 | 2 |
|
3 |
| -<p>Чтобы выводить адекватное сообщение об ошибке, мы будем все проверки осуществлять с помощью assert и перехватывать исключения.</p> |
| 3 | +<p>To display an appropriate error message, we will perform all checks using assert and catch the exceptions.</p> |
4 | 4 |
|
5 |
| -<p>Для этого напишем вспомогательный метод поиска элемента в нашей базовой странице BasePage, который будет возвращать нам <code>True</code> или <code>False</code>. Можно сделать это по-разному (с настройкой явных или неявных ожиданий). Сейчас воспользуемся неявным ожиданием.</p> |
| 5 | +<p>For this purpose, let's write a helper method to find an element on our BasePage, which will return either <code>True</code> or <code>False</code>. This can be done in various ways (with explicit or implicit waits). For now, let's use implicit waiting.</p> |
6 | 6 |
|
7 |
| -<p>1. В конструктор BasePage добавим команду для неявного ожидания со значением по умолчанию в 10:</p> |
| 7 | +<p>1. In the constructor of BasePage, let's add a command for implicit waiting with a default value of 10:</p> |
8 | 8 |
|
9 | 9 | <pre><code class="language-python">def __init__(self, browser, url, timeout=10):
|
10 | 10 | self.browser = browser
|
11 | 11 | self.url = url
|
12 | 12 | self.browser.implicitly_wait(timeout)</code></pre>
|
13 | 13 |
|
14 |
| -<p>2. Теперь в этом же классе реализуем метод <code>is_element_present</code>, в котором будем перехватывать исключение. В него будем передавать два аргумента: <em>как </em>искать (css, id, xpath и тд) и собственно <em>что </em>искать (строку-селектор). </p> |
| 14 | +<p>2. Now, in the same class, we'll implement the <code>is_element_present</code> method, where we will catch the exception. We will pass two arguments to it: <em>how </em>to search (css, id, xpath, etc.) and <em>what </em>to search for (the selector string). </p> |
15 | 15 |
|
16 |
| -<p>Чтобы перехватывать исключение, нужна конструкция <code>try/except</code>: </p> |
| 16 | +<p>To catch the exception, we need a <code>try/except</code> construct: </p> |
17 | 17 |
|
18 | 18 | <pre><code class="language-python">def is_element_present(self, how, what):
|
19 | 19 | try:
|
20 | 20 | self.browser.find_element(how, what)
|
21 |
| - except (имя исключения): |
| 21 | + except (name of exception): |
22 | 22 | return False
|
23 | 23 | return True</code></pre>
|
24 | 24 |
|
25 |
| -<p>Чтобы импортировать нужное нам исключение, в самом верху файла нужно указать: </p> |
| 25 | +<p>To import the required exception, you need to specify the following at the very top of the file: </p> |
26 | 26 |
|
27 |
| -<pre><code>from selenium.common.exceptions import имя_исключения</code></pre> |
| 27 | +<pre><code>from selenium.common.exceptions import exception_name</code></pre> |
28 | 28 |
|
29 |
| -<p>Отлично! Теперь для всех проверок, что элемент действительно присутствует на странице, мы можем использовать этот метод. </p> |
| 29 | +<p>Great! Now, we can use this method for all checks on the presence of an element on the page. </p> |
30 | 30 |
|
31 |
| -<p>3. Теперь модифицируем метод проверки ссылки на логин так, чтобы он выдавал адекватное сообщение об ошибке: </p> |
| 31 | +<p>3. Now, let's modify the method for checking the login link so that it provides an appropriate error message: </p> |
32 | 32 |
|
33 | 33 | <pre><code>def should_be_login_link(self):
|
34 | 34 | assert self.is_element_present(By.CSS_SELECTOR, "#login_link_invalid"), "Login link is not presented"</code></pre>
|
35 | 35 |
|
36 |
| -<p>Запустите тесты и посмотрите, что вывод об ошибке стал более понятным: </p> |
| 36 | +<p>Run the tests and see that the error output has become more understandable: </p> |
37 | 37 |
|
38 | 38 | <pre><code>pytest -v --tb=line --language=en test_main_page.py</code></pre>
|
0 commit comments