andhapp Random ramblings

Installing Java on Mavericks with homebrew-cask

Years ago, when I used to develop on a Windows machine, a senior colleague of mine told me - “Apple’s version of Java is slightly behind”. Ofcourse, I was oblivious to the control tech giants can force on it’s users. Anyways, whilst setting up my machine for some Android development, I had to install Java. I had JRE installed to run applets (who does that now?) in the browser, but for android development you need JDK.

While googling, I stumbled upon homebrew-cask and I was totally blown away. You can literally download the binaries and have them install by just running one command on the Terminal.

brew cask install virtualbox

Shut up and take my money!

I ran commands for VirtualBox and Genymotion and it worked like a charm. However, when it came to installing Java, it fell flat on it’s face. Homebrew-cask downloaded the java binary to this directory:

/opt/homebrew-cask/Caskroom/

but just left it there. I was still stuck on ‘no Java’ issue, so I just double-clicked the binary to install it. It installed properly and I was on my way to some andorid enlightenment, but the process revealed two things that need more investigation:

1. homebrew-cask has a bug somewhere as it failed to install Java binary.

2. Now, it’s easier to just run one command and install Java. I always find it really challenging to find the right binary on Apple’s site to install.

Hope it helps anyone needing Java, and unknown to homebrew-cask.

cucumber-js, appium, WebDriverJs and Javascript Promises

I have mixed feelings for Appium. At times, it works beautifully, and then suddenly falls flat on it’s face, leaving you frustrated. Nevertheless, once you know how it’s wired up with other related tech, you feel comfortable and willing to debug the issues it throws at you. Before you ask, I had to write features to test a hybrid mobile application, currently just targeting iOS. This post just describes my experience and some gotchas. It doesn’t go into details on how to set up appium with cucumber-js, because there are plenty of other blog posts on that subject.

cucumberjs

Cucumber is a defacto for writing features and the good news is that’s what I used. It’s no different in terms of syntax to it’s ruby counterpart. You need to set-it up with Appium, and WebDriverJs, but there are plenty of articles on the subject.

appium

Appium is a test automation framework that implements Selemium WebDriver interface using instruments for iOS, and Android. It implements the Selenium WebDriver spec, so any selenium compatible client web drivers work fine with it. Appium exposes selenium-compatible API over HTTP, and selenium-webdriver communicates over the exposed end-points. Now, Appium not just implements WebDriver spec, it also augments it in interesting ways. Appium uses instruments to inspect iOS code.

WebDriverJs

WebDriverJs is the final piece of this puzzle. It’s not much different from any other web driver. However, it does have a couple of interesting things. First, it implements a promise manager in the background which ensures that you write async code in a sync manner. For example, with promises you will write code like this:

   driver.loadWebApp().
    then(login).
    then(openUserPreferences);

but, with WebDriverJs the promise manager, you don’t have to worry about the verbosity, you can simply do:

    driver.loadWebApp();
    driver.login();
    driver.openUserPreferences();

The promise manager ensures the order is maintained giving you the same behaviour as callbacks. Now, you would like to use the same promise manager with your own code, but the promise manager is not really exposed publicly. Well, it’s exposed but I couldn’t really find a way to use it with the way we have wired it up in our project. However, if you’d like to take use of it then you can do something like this:

driver.call(myFunc);

The call method delegates calls to the promise manager.

Feature testing gotchas

1. You will see the following error intermittently, and this just means restart appium:

SessionNotCreatedError: A new session could not be created 
error: Failed to start an Appium session, err was: Error: Requested a new session but one was in progress

2. If you cancel running your features in the middle, and then try again, they won’t run. You will have to restart appium.

3. Finally, running features are very time-consuming, since appium fires up the emulator and evaluates the features against it.

Grep, Sed and Grasp: Replace strings in files

This week, I was refactoring some of the Javascript code, which involved renaming files, and then amending the code that was requiring those files. The project uses requirejs, so the filename is kinda pivotal. I had read about Grasp and saw this as an opportunity to do some refactoring voodoo. Grasp is very powerful and there is ample documentation, but when you don’t have enough time, you want to look at some examples that tell you exactly what to do. Rails API docs have spoiled me for good. Grasp still needs to be conquered, perhaps another time.

Here’s the one-liner that I used to accomplish this task. I used it as part of a script so the values were dynamically substituted:

 grep -rl '<oldstring>' <path> | xargs sed -i '' 's/<oldstring>/<newstring>/g' $1

<oldstring> is the string you want to replace

<newstring> is the string you want to replace <oldstring> with

<path> is where you want grep to start working from

Grep recursively - -r finds the files and -l pipes the file path in to xargs - which executes the sed command for file it receives. $1 references the file path received via xargs. This one-liner doesn’t create a backup file for every file changed, however, if you want to create a backup file as well in the same directory as the file changed, you can use the following, slightly different one-liner:

grep -rl '<oldstring>' <path> | xargs sed -i.bak 's/<oldstring>/<newstring>/g' $1

If I knew grasp well, I’d have used that instead, but for now, grep and sed are good enough.

Xcode shenanigans

Recently, I’ve been working on a Phonegap application and just wanted to share some Xcode gotchas.

1. Xcode’s crash reports are saved in /Library/Logs/DiagnosticReports/. This comes handy when Xcode crashes on you, sometimes. It doesn’t happen often, but for instance you have an Xcode project open and you moved the directory where the project was with Xcode open all that time, it will crash. Just be aware.

2. Xcode saves all the user project settings in another place. This is in ~/Library/Developer/Xcode/DerivedData directory. If you don’t want Xcode to remember any of your settings (like what windows were open in the project for next time), or you just want to clear the project settings cache, just delete this directory and you’ll be good as gold.

3. Finally, Xcode is very clever like any other IDE out there. It remembers the projects you’ve opened recently and keeps a reference to them. Now, if you’ve moved a directory with Xcode still open, it may crash. Launching Xcode post crashing will just result in it crashing again, because it’s trying to be clever, as expected, and trying to open the projects you had open before it crashed the last time. It’s just like stack too deep bug that we are familiar with.

Xcode is a nice IDE for what it does, but sometimes if you stray from the golden path you can end up in a back alley not knowing where to turn to and by the way, these are three stack overflow answers in one post.

Deploy apps with Mina

I recently decided to setup a dedicated website for my company as opposed to just using my personal one. It’s a static site, with barely any content right now.

Now, I usually rely on capistrano for deployment, but since capistrano had recently undergone a major facelift, I sensed it as an opportunity to try Mina. I have known about the gem since it’s very beginning, but the pressures of modern web developer life means less time for playing around, and more for getting things done. In other words, don’t change the deployer if it ain’t broken.

Mina is very similar to Capistrano, except one small yet pivotal fact. Instead of running commands one-by-one over SSH, it creates a bash script from your configuration and executes it remotely as one SSH command. Sounds astute to me. It has the same configuration and deploy setup as Capistrano, and anyone with some Capistrano experience will pick it up pretty quickly.

One thing that really stuck out for me and propelled me to write this post is it’s reliance on the existing SSH set-up on your machine. For example, In Capistrano, one has to explicitly set up the ssh forward agent in the deploy script itself. But, in Mina, one just needs to specify forward agent in their SSH config, and Mina will pick that up. It’s quite logical, no idea why it wasn’t the case for Capistrano.

Anyways, hope this will motivate you guys to try Mina as well.