Find screen resolution with Selenium RC and WebDriver

Recently we were running into some problems with our Selenium tests. We have a bunch of tests using legacy Selenium RC, and we’re converting them over to WebDriver. If you’ve done this yourself, you may have noticed how WebDriver doesn’t quite behave the same as the old Selenium.

One particular difference that has caused us a lot of grief is that WebDriver can’t interact with elements on the page unless it can “see” them. This may arguably be a good thing, because it actually tests that your page is usable at the screen resolution and browser sizes that you’re testing with, but it does cause some hard-to-fix tests. The kind that work great when you run them locally, but fail unpredictably on remote systems through Selenium Grid.

We needed a way of checking the browser size and screen resolution on the remote host, and while WebDriver does offer some built-in methods to get the browser size, there’s nothing for screen resolution. JavaScript to the rescue! Selenium RC and WebDriver can both run JavaScript code in the browser, and the browser knows the screen resolution. The answer is in the screen.width and screen.height variables.

Determine the Screen Size

Here’s some code to figure out the screen with and height. It’s in Java, but shouldn’t be too hard to translate to other languages that are supported by Selenium/WebDriver.

Using WebDriver

// Here, 'driver' is your instance of some subclass of WebDriver.
JavascriptExecutor js;
if (driver instanceof JavascriptExecutor) {
    js = (JavascriptExecutor)driver;
}
int width = (int)js.executeScript("return screen.width");
int height = (int)js.executeScript("return screen.height");

Using Selenium

// Here, 'selenium' is an instance of DefaultSelenium or WebDriverBackedSelenium
int width = (int)selenium.getEval("screen.width");
int height = (int)selenium.getEval("screen.height");
Posted in Programming | Tagged , | Leave a comment

Restoring a Clonezilla clone of CentOS 5.5

I recently had the experience of restoring a backup made using Clonezilla. It’s a nice bootable utility that lets you backup and restore entire system drives, among other things. As they say, a backup solution is only as good as its restore, and that’s where I ran into a little trouble.

Panic!

I had cloned a CentOS 5.5 Linux system and needed to restore it to slightly different hardware. The restore completed without a hitch, but the system had a kernel panic at boot. I tried various workarounds for it, and ultimately this was what worked:

The Fix

  • Using a CentOS 5.5 install DVD, boot into rescue mode (type linux rescue at the boot prompt). It’s very important here to use the same version of install DVD as the system you are restoring. Same version number and same bit-ness (32-bit or 64-bit).
  • Once at a prompt, chroot to your system and go to /boot:
    chroot /mnt/sysimage
    cd /boot
  • Backup your existing initrd file for safety. It’ll be called initrd-[kernel version].img. If you have a few of them, just backup the one with the kernel version you’re using.
    cp initrd-[your kernel].img initrd-[your kernel].img.backup
  • Make the new initrd file. The key, for me, was to include the --force-lvm-probe and --force-raid-probe flags which don’t appear in the man page for mkinitrd but really should.
    mkinitrd -v --force-lvm-probe --force-raid-probe \
             initrd-[your kernel].img \
             [your kernel]
    

    Tip: Instead of typing out the whole (error prone) kernel version, just tab-complete the name of your initrd-[kernel].img.backup filename and delete the .backup from the end. Then do the same again, and remove the .img.backup from the end and the initrd- from the front.

    Another tip: Don’t type it out on 3 lines like it’s shown here ;)

That should do it! Reboot into your restored system.

Posted in Uncategorized | Leave a comment

Increase Productivity by Eliminating Depth First Search

In the computer science world, there is a strategy called depth-first search. It’s a method of iterating through nodes of a tree or graph. It can work well for things like solving a maze: try each path as far as it will take you, and when you hit a dead end, go all the way back and try another path. If you do this systematically, you’ll find your way out of the maze.

It is awful for getting anything done in the real world, though. xkcd agrees.

Ever had a day like this?

“I really want to work on that side project when I get home,” you say. Then you get home around 6pm and realize that you have to eat dinner first. Cereal or peanut butter and jelly are too boring, and you can’t think of anything that sounds good. So you search Google for a few minutes and find a recipe for something like Grilled Chicken in Spiced Yogurt. Awesome!

But you have no yogurt. And it needs a couple spices you don’t have. Time to run to the store! Drive there, grab the ingredients, then realize that your car is very low on gas. Just one more quick stop at the gas station on the way home. 40 minutes later you’re back home and ready to start cooking. Instead of the advertised 35 minutes, dinner takes about an hour to make (it was tricky, and hey, you never made it before!).

By the time you’re done eating it’s 8pm and you have to clean the dishes. Ugh. Now it’s 8:30pm, and TV is looking a lot better than that side project…

The oh-so-common “click on every link” trap

You’re at work, and you really need to write a report for that meeting at 2pm. But boy, is it boring. And it’s only 9am. A quick break catching up on blogs won’t hurt. You head over to Cal Newport’s blog and see that he’s posted a new article. Cool!

Before long, you find yourself engrossed with the idea of deliberate practice and how helpful it seems. You follow the first link in Cal’s post, leading to another one of his posts. Then you click the first link you see. Then the first link in the next one… Before you know it, you’re knee-deep in link-following and even if you click-and-hold your Back button you won’t be able to find your way out of Cal’s blog because that Back button dropdown only holds 10 items. So you hit that Back button and click on the second link in the post…

Recognizing DFS in Your Own Life

Did you notice a few similarities in the two examples? How you kept taking the first possible path that arose, without stopping to think where it might lead? That’s depth-first search, right there.

Before you got down here, did you already visit the linked depth-first search article in the first paragraph? And the Grilled Chicken in Spiced Yogurt recipe in the first example? And maybe made that recipe for dinner?

I bet you can identify a few times when you’ve gone off on unexpected tangents like these. I certainly can. We all do it. Perhaps we can shift the blame to today’s inundation of endless stimulation. Between TV, Web surfing, and never-ending “ASAP” requests at work, we train ourselves to never stick with one thing for too long. But learning to recognize it (and stop it) will save you countless hours in the long run.

So next time, before you click that link or spuriously add a new item to the top of your to-do list, think of depth-first search. Pause for a moment to ask yourself if the idea that just popped into your head really needs to be done right now before everything else. It probably doesn’t.

Posted in Productivity | Leave a comment

BMW Performance Center Delivery

A couple days ago I had the pleasure of taking delivery of a 2012 BMW 128i at BMW’s Performance Center in Spartanburg, SC. It was an experience to remember.

About Performance Center Delivery

On every brand new, custom-ordered BMW vehicle, BMW offers customers the option to take delivery at the Performance Center (instead of taking delivery at the dealer). At the time of this writing, it includes a number of perks above and beyond a usual delivery experience:

  • Free night in the Marriott Greenville hotel
  • You can bring a guest
  • Free dinner at Marriott Greenville’s restaurant, Giatu
  • Free breakfast buffet at Giatu
  • Free lunch at the Performance Center
  • Transportation from the airport (in a BMW, of course)
  • Transportation to the Performance Center on delivery day
  • A free tour of BMW’s Spartanburg factory, where they build X3, X5, and X6 vehicles
  • Personalized, one-on-one delivery of your vehicle, where one of the Performance Center’s driving instructors will explain every knob and function in your new vehicle
  • A couple hours on the track at the Performance Center, including:
    • A mini autocross-style course
    • Emergency braking exercises
    • Experience driving in wet conditions with and without traction control
    • A “hot lap” in an M5, driven by one of the Performance Center’s driving instructors
    • Offroading in an X5

It is a ton of awesome rolled into one day, and I highly recommend it if you have the chance.

My PCD Experience

Despite a couple delays, we arrived at Greenville airport (KGSP) around 1:30pm on Thursday, August 4th. A quick call to Marriott and a driver was dispatched to pick us up. He arrived in a tan BMW 5 Series Gran Turismo, helped us load all our bags, and drove us over to the hotel. Our room had a nice view of the pool below.

The driver, Matt, was gracious enough to give us a ride to and from the nearby mall for some shopping in the afternoon. It turns out malls in Greenville are the same as malls everywhere, but it was fun to get out for a bit. We cashed in our free drink coupons and spent some time lounging by the pool when we got back, and then went to dinner. The filet mignon at Giatu was excellent. The entire hotel experience was great; the staff were all very accommodating and friendly.

Delivery day began at 6:30am. Most people said that they could barely sleep the night before delivery; I, on the other hand, could barely get out of bed. It was extremely comfortable, and I was way tired after a day of travelling. We took advantage of the breakfast buffet and boarded the shuttle to the Performance Center at 7:45, and arrived around 8 to be welcomed by the staff.

We started the day in the classroom. Donnie Isley, one of the driving instructors, went over the exercises we’d be doing and emphasized NYCNot Your Car. We would be driving BMWs similar to the models we were picking up, and were told not to worry about the tires and brakes. We were intended to push these cars to their limit to learn how our own cars would react in similar real-world situations.

We were split up into groups, a few couples per instructor, and headed out to the track to saddle up in (not) our cars. Instead of an instructor riding along in each car, he had a radio tuned to the same channel as one clipped to our lefthand door, which turned out to work very well. Our instructor was Ray Helms, who turned out to be a great teacher and one heck of a nice guy.

The Performance Center doesn’t have any 128i cars, but they do have a bunch of 135i’s and we were placed in one of those. Not exactly my car, but I’m not complaining. The car was a blast to drive.

Autocross

The first event was a mini course involving a slalom, a tight turn, and a decreasing radius turn. Ray led us through the course in his M5 at a slow pace first, pointing out things to watch for, and then we got a go at it ourselves. We had about 5 laps per driver before switching places, with Ray egging us on to push it faster and brake harder with each consecutive lap. My girlfriend followed his advice to a fault, and we spun out into the grass when we tried to take the tight corner a little too fast. Awesomeness. NYC, remember?

Braking

Next was a braking exercise, where we approached a lane of cones and slammed on the brakes at the first cone to see how fast we could stop. At first we only got up to 35mph before hitting the brakes, but by the end we were up to 50mph and it was taking significantly longer to stop.

We learned that the ABS wouldn’t help us stop any sooner, but would let us continue to steer while we held the brakes. We also learned that at 50mph, a small distraction (in the form of Ray chatting it up on the radio and then yelling “hit the brakes NOW!”) added a second or more to our reaction time and nearly doubled our stopping distance.

There was a brand new 1M parked alongside the track.

Skidpad

The final self-driven event on the track was a spin around the skidpad. For this exercise we switched cars into a 335d dedicated to this segment. This time Ray rode with us individually.

We disabled traction control completely, and began to drive around the slick skidpad at 20mph. After a few seconds of this, Ray told us to floor it, which we did, and promptly spun out. We tried that a couple times, then we turned traction control back on. With DTC enabled, the car refused to spin. Countersteering got us back on track.

After the track exercises, we were split into two groups: one would take delivery of their new vehicle, and the other would do off-roading and a factory tour. After lunch, the groups swap. I was in the group to take delivery before lunch.

Delivery

The moment I had waited 4 months for had finally arrived! It turned out that Ray was our delivery specialist too. He led us into the room where my shiny blue 128i sat. All I could say was, “wow.” We snapped a few pics and then Ray spent the next hour and a half going over every little feature on the car, helping my girlfriend and I pair our bluetooth phones with the car (an iPhone and an LG Rumor2), and setting up BMW Assist.

Lunch

BMW is serious about performance, but I can vouch that they’re pretty good at lunch too.

The Performance Center has a chef on staff who gets to choose what to make each day. This particular Friday was pizza day, and it was delicious. I had the BBQ chicken pizza and a fruit cup. We even had tables reserved for us. Ray joined us for lunch.

The 1M was plastered everywhere. I thought this poster was especially fitting.

Hot Lap

After lunch, each delivery group was given a chance to go for a ride in an M5 for a “hot lap” with an instructor driving. This was (aside from delivery) the highlight of the day. Again we were paired up with Ray. It turned out he knew how to drive an M5 real well, too. I would imagine it’s uncommon for one delivery couple/person to be paired with one instructor for the entire day, but I feel like we lucked out. Ray was great.

I think the video speaks for itself.

Understandably, they go through a lot of tires at the Performance Center…

Off-Roading

Next up was an off-roading course in an X5. This was a ton of fun, and we got to take the X5 up and down some really steep rocky hills to see what it could do. We also drove it through almost 2ft of water, over some big bumps, and got 2 tires airborne at one point. It was a blast.

Factory Tour

We ended the day with a tour of BMW’s factory in Spartanburg, right across the street from the Performance Center. X3, X5, and X6 vehicles are built there, and we got to see how they come together. Pictures were not allowed during the tour — the only part of the whole day where pictures weren’t allowed.

Sadly, a large chunk of the factory’s workforce was sent home early that day, and so part of the plant wasn’t in production mode. Our tour guide seemed extremely surprised by this and was very apologetic. She said it was the first time she’d ever seen it happen. We found out from one of the workers that the factory apparently ran out of doors, and so production couldn’t continue that day. Given how accommodating they were with everything else, I have little doubt they’d offer a free factory tour if I ever wanted to make the trek back to Spartanburg.

We did see a bunch of X vehicles sitting on a conveyor waiting for various parts to be installed. We also got to see a bunch of big robotic arms in action, piecing together the undercarriage of the vehicles and welding things.

Conclusion

Performance Center Delivery is the only way to take delivery of a new BMW. Well, that and European Delivery, which I hear is also great.

It was absolutely worth the wait.

We drove the 128i home to Boston over the weekend, winding along the Blue Ridge Parkway and stopping in Roanoke, VA for the first night and Lancaster, PA for the second night. One of the overlook points along the Parkway had this magnificent view…

A whole lotta trees and that sign. Wooo!

3 tanks of gas and 1047 miles later we made it home, and almost done with break-in!

Posted in Cars | Tagged , , | Leave a comment

Beware #pragma pack

#pragma pack is a compiler directive for changing the alignment of data structures in C/C++. The Wikipedia page on data structure alignment has lots of info about it so I won’t bore you with the details. Essentially, #pragma pack will let you decide the byte alignment of your data structures. Maybe you feel that your particular platform is fastest when reading values aligned on 4-byte boundaries, so you set #pragma pack(4) before you define something:
// myheader.h
#pragma pack(4)
struct foo {
   char type;
   int answer;
   double pi;
};
But be careful! If you now go and #include "myheader.h" somewhere else, it will affect the byte alignment in that file too! This can cause all sorts of weird problems. In one case, I didn’t notice it for a while until I started experiencing random segmentation faults and crashes, and finally ran my program through Valgrind. I noticed that when I allocated an instance of one of my classes, it was allocated 8 bytes less than it needed. So I would do:
MyThing *thing = new MyThing();  // sizeof(MyThing) == 80 bytes
But Valgrind would warn me that it had only allocated 72 bytes! This led to unhappy times when I later tried to write to the last member variable in MyThing. There are 2 solutions here:
  1. Remove any #pragma packs from your code entirely.
  2. If you have #pragma pack in your code, you might want to take a look at why it’s being used and see if it can be removed. Perhaps the alleged performance benefits really don’t exist, or maybe the packing actually hurts performance. Do some benchmarking to see if changing the byte alignment is necessary.
  3. Wrap use of #pragma pack with a pair of #pragma pack(push) and #pragma pack(pop)
  4. If you can’t get rid of the #pragma pack for some other reason, you should absolutely make sure you are setting the byte alignment back to its default when you’re done defining a structure or class. Something like this will take care of it:
    #pragma pack(push)   // save the current packing
    #pragma pack(4)
    
    struct foo {
       ... // lots of variables
    };
    
    #pragma pack(pop)   // restore the old packing
    
    This saves you from having to know what the packing was in the first place.
Hopefully this tip will save someone a few hours of debugging.
Posted in C++, Programming | Leave a comment

First Post!!1

I am now officially a blogger. Or Blog Master, which I think is better. I’ve owned this domain, binarynirvana.com, for quite a while. In a former life, it was home to a Counter-Strike server and a Team Fortress Classic server, as well as a web server with a forum and an installation of HLstats for players to track their performance. I played as Syko back then. It was a lot of fun. That was back in high school. Fast-forward 10 years and I’m no longer hosting the site in my parents’ basement on my then-screaming-fast 768/384kbps Speakeasy DSL connection. I’m now working full-time as a Software Engineer, sporting a screaming-fast 15/5 FiOS connection. The Internet has evolved a lot since then. So I thought it time to join the 21st century and start a blog. What will I put here? Who knows. For starters, I expect to be posting about day-to-day problems (and maybe even the occasional solution). Linux, programming, cooking, cars, general aviation, music… the list of possibilities is long and varied. Only time will tell what will actually come out of this blog. Stay tuned.
Posted in Uncategorized | Leave a comment