R quick tips and tricks

Recently I’ve been working with R and Shiny, here are some code snippets that I’ve found useful:

When you need to cut the string and get the stuff before the space:

temp1 <- sub(" .*", "", your_string)

When you need to cut the string and get the stuff after the space:

temp2 <- sub(".* ", "", your_string)

When you need to cut the string and get the number from the string:

temp3 <- as.numeric(gsub("\\D", "", given_reward_name))

When you need to cut the string and get something between different characters, in this example [ and :

temp4 <- sub(".*\\[(.*):.*","\\1",given_reward_name)

When you want to convert the list to the data frame:

df <- data.frame(reward_name = unlist(your_list))

When you want to split one column in to two:

df <- <- str_split_fixed(df$name_of_the_column, " ", 2)

When you want to split a column by a character:

df <- str_split_fixed(df$name_of_the_column, " ", 2)

When you want to check if there is something in a something:

if(is_this_in %in% df$name_of_the_column)

OpenCV on Windows + Pip

I’ve had some minor issues with getting OpenCV to work on my Windows machine and all guides were not helping.

I was following these instructions found on OpenCV official website:

http://docs.opencv.org/3.2.0/d5/de5/tutorial_py_setup_in_windows.html

After installing python, numpy and matplotlib and trying to:

import cv2

I was greeted with the following error:

RuntimeError: module compiled against API version 0xa but this version of numpy is 0x7 windows

After some googling, the reason was quite obvious. I needed to upgrade my numpy version.

Somehow that appeared to be hard to do without Pip. How to get Pip

  1. Save this as get-pip.py https://bootstrap.pypa.io/get-pip.py
  2. Open up your cmd
  3. Type python get-pip.py
  4. You should be fine now
  5. To double check it’s indeed installed:
    1. Go to the location where your python and Scripts folder inside it C:\python27\Scripts
    2. You should see “pip” named .py file

After installing pip it should be very easy to get numpy installed.

pip install numpy

and to upgrade it to the latest version:

pip install numpy --upgrade

After which when I open my Python IDLE and write

import cv2
print cv2.__version__

I get no errors like the original tutorial said.

Selenium – Chrome driver, element is not clickable at point.

While automating a website I’ve stumbled acorss a problem that seemed only to happen on Chrome browser alone.

The problem was that, after finding a webelement that I wanted to click, the chrome driver threw out: “Element is not clickable at point (x, y)” . The thing is that it happened only on Chrome, for example using gecko-driver I had no problems with finding that element and clicking on it.

Seems like it is a known bug, where if the element is behind or under another element chrome driver tries to click in the middle of it and it fails.

The solution was found here:

https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/2766

Solution that worked for me:

WebElement button = driver.findElement(By.id("buttonID"));

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", button);

button.click();

Selenium 3 – Gecko driver, Firefox Browser

After upgrading from Selenium 2.x to Selenium my tests stopped to work. The error that I was getting was this:

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property;

After taking a look at change log I’ve discovered that the support of Firefox browser is now through the Gecko driver. That was a new thing for me, so after googling around and trying to solve the issue, this was what I’ve found.

Adding this helped me solve the problem:

System.setProperty("webdriver.gecko.driver","path of geckodriver");
WebDriver driver = new FirefoxDriver();

 

Selenium – How to grab Dojo listboxes

Lately I was writing automation tests for a web applications that was using Dojo Toolkit. One of the problems that I’ve encountered was that I was not able to select an item from what seemed like a regular dropdown menu. With normal dropdowns that have “select” tag in html Selenium has a simple way to grab them

Select dropdown = new Select(driver.findElement(By.id("identifier")));

and you can select the values either by

dropdown.selectByVisibleText("TEST ");

or by index

dropdown.selectByIndex(1);

But when I was digging deeper it seemed like the dojo element handled “dropdowns” in a different way. They are tables with only one row but many columns. The problem comes when you try to select the values in them.

When you go through the elements in the table one by one and print out the text of each item in the table the getText() method prints either “null” or an empty string. The solution to this problem is that the elements found in that table are found under dijitMenuItemLabel class and in my case had an id attirbute with

In my case I wanted my code to be flexible and select always the last item in the dropdown menu. The idea behind the next code snippet is that it cycles through an array of all the elements that i found previous, matches them by the id of the element and does something based on which element it matches.

In this case if it finds an element that has an attribute id that contains “Select” it goes in to this:

// find the dropdown button e being an element that we currently going through
WebElement dropDownButton = driver.findElement(By.xpath("//table[@id='" + e.getAttribute("id") + "']/tbody/tr/td"));
// click on it
dropDownButton.click();
// find all of the elements inside the dropdown menu,
List<WebElement> test = driver.findElements(By.xpath("//td[contains(@id, 'text')]"));
// find the last element in the dropdown list and click on it,
test.get(test.size() - 1).click();