понедельник, 18 декабря 2017 г.

IT - AQA: Selenium+javascript: node.js - wotk with wait and assertion (updated)



Examples:
1)  Wait until element will shown:
const until = selenium.until;
driver.wait(until.elementLocated(locators.detailsGenresDropDown), 5000);

2) implicitlyWait() in javascript selenium bindings accepts a single argument - a timeout in milliseconds:
driver.manage().timeouts().implicitlyWait(20000);

3) page is loaded:
java:  driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
js: driver.manage().timeouts().pageLoadTimeout(10);

4) The code above makes browser wait for 5 seconds after clicking the button (+1).
driver.findElement(webdriver.By.name('btnCalculate')).click().then(function() {
    driver.sleep(5000);
});

If using webdriverJs (node.js),

5) What's the equivalent of Java's Thread.sleep() in JavaScript? (+1)
The simple answer is that there is no such function.
The closest thing you have is:
var millisecondsToWait = 500;
setTimeout(function() {
    // Whatever you want to do after the wait
}, millisecondsToWait);
Note that you especially don't want to busy-wait (e.g. in a spin loop), since your browser is almost certainly executing your JavaScript in a single-threaded environment.
Here are a couple of other SO questions that deal with threads in JavaScript:
And this question may also be helpful:
6) Just sleep for several seconds:// Page has been loaded, now go to sleep for a few seconds.
driver.sleep(3000); 
 

Assertion

Examples:

1)    client.getTitle().then(function(title) {
        assert.ok(title.indexOf('test — Яндекс: нашлось') > -1, 'Ничего не нашлось :(');
    });


Links
WebDriver: Advanced Usage - Explicit and Implicit Waits
Документация по Selenium.
Документация по WebDriver.js;
Черновик спецификации WebDriver API;
Webdriver selenium js docs

воскресенье, 3 декабря 2017 г.

AQA: Selenium: Javascript collection of tricks

Collection of knowledge about selenium + js 



Console на полную: console, log, info, warn, assert, trace (link1)
Clipboard copy and paste (link1)

Insert data in textarea:
- how to send a page source to a text area (link question, link with java examples). Used code:
String page=driver.getPageSource();
WebElement e=driver.findElement(By.xpath("textarea xpath");
e.sendKeys(page);
-  Using sendKeys( ) command for entering multiple lines of text into a Text Area field (link). Used code:
const text2 = "My super long text \n string to be typed into \n textarea element";
\b - backspace
\t -  tab
\n - newline
\f -  form feed
\r - carriage return
\" - double quote
\' -  single quote
\\ - backslash
- Using clipboard. (link)
Clipboard.SetText(trgt);
myTextArea.SendKeys(OpenQA.Selenium.Keys.Control + "v");
- Learn how to upload a file using Selenium Webdriver with Sauce Labs’ guide.(link1, link2)
- C помощью Selenium Webdriver ввести текст в frame редакторa tinymce (link1):
driver.switchTo().frame("news_globalize_translations_attributes_ru_content_ifr");
driver.findElement(By.id("tinymce")).sendKeys("Text");
driver.switchTo().defaultContent();

JavaScriptExecutor in Selenium WebDriver With Examples (original link)

JavaScriptExecutor in Selenium WebDriver:
In general, we do click on an element using click() method in Selenium.
For example:
Sometimes web controls don’t react well against selenium commands and we may face issues with the above statement (click()). To overcome such kind of situation, we use JavaScriptExecutor interface.
It provides mechanism to execute Javascript through Selenium driver. It provides “executescript” & “executeAsyncScript” methods, to run JavaScript in the context of the currently selected frame or window.
There is no need to write separate script to execute JavaScript within the browser using Selenium WebDriver script. Just use predefined interface named ‘Java Script Executor’. We need to import the below package in the script.
Package:
Syntax:
Script – The JavaScript to execute
Arguments –
The arguments to the script(Optional). May be empty.
Returns –
One of Boolean, Long, String, List, WebElement, or null.
Let’s see some scenarios we could handle using this Interface:
  • To type Text in Selenium WebDriver without using sendKeys() method
  • To click a Button in Selenium WebDriver using JavaScript
  • To handle Checkbox
  • To generate Alert Pop window in selenium
  • To refresh browser window using Javascript
  • To get innertext of the entire webpage in Selenium
  • To get the Title of our webpage
  • To get the domain
  • To get the URL of a webpage
  • To perform Scroll on application using  Selenium
See Also: How To Scroll Web Page Down Or UP Using Selenium WebDriver
  • To click on a SubMenu which is only visible on mouse hover on Menu
  • To navigate to different page using Javascript
  • To find hidden element in selenium using JavaScriptExecutor
Below program will guide you to handle some of the scenarios we do use in while writing scripts.
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.

 


SEO: Эксперимент: как Яндекс и Google учитывают ключевые слова в URL

Эксперимент: как Яндекс и Google учитывают ключевые слова в URL Эксперимент: как Яндекс и Google учитывают ключевые слова в URL Дата пу...