Ruby - Program navigator module - ruby

I have a Ruby program that uses a webdriver (Watir) to walk a page and perform tests alongside a BDD suite called RSpec.
I'm trying to optimize it for a slow server by improving its ability to navigate efficiently. Thus far It has been creating a new browser session for each test package, then closing it afterwards. This is very inefficient because it hits the login page again for every instance.
Of course, I don't want to hard-code navigation instructions into the tests because adding new spec files may change the order they are executed in, and not every page of the webapp has the main navigation bar, so navigation may need to change based on the page the last spec left the browser on.
I need some kind of master library or module that will take what page the program is at and what page it wants to go to, then bring the browser to that page so it can begin testing. What is the best way to do this?
I'm not fantastically experienced so I'd love input from more seasoned developers. Should I have each page be a class? Should I just stick with closing browsers after each test packet? Should I manually code brute-force methods (gotoPage1FromPage2)?
Okay, that last one was a joke. Seriously though, what is the best way to do this?

You are exactly correct about the difficulties of maintaining state in your tests. Shutting down a browser between each session is the best way to make sure that you always know the state of the browser at all times for a test. Saucelabs goes so far as to spin up a new virtual machine for each of the tests they run. Ideally you decrease test time by running multiple tests in parallel.
I'm not certain I know what you mean by "test package" or how many times that means you are starting a new browser and logging in, but... another thing to consider investigating is whether you can set a cookie or use oauth to log in without having to use the navigation. I've worked at places that allowed admin logins for their staging environments by passing a parameter in an url.
Your tests should be clear in their intention, which typically means your Page Object implementation does not know about what comes before or after the actions you are taking. You should be able to look at the RSpec code and reproduce exactly what it is testing. Abstracting methods for taking you from one place to another magically in the background is not a good idea.
Best practice used to be having methods from one Page Object return new Page Objects. So users could write methods like this in their tests: LoginPage.new.login.view_account.edit_address. Many of us have been bitten by this approach. Plus it isn't as easy to read as doing something like this:
LoginPage.new.login
HomePage.new.view_account
AccountPage.new.edit_address
This doesn't prevent you from using #visit methods as needed to navigate between Page Objects.

Related

Testing links in a web app

I need to test links inside a web application. I have looked at a couple tools (Xenu, various browser plugins, link-checker(ruby)). Nothing quite fits my needs which I will detail below.
I need to get past a login form
test needs to be rerun for different types of users (multiple sets of login credentials)
would like to automate this under a ci server (Jenkins)
the ability to spider the site
Does anyone have any ideas? Bonus if I can use Ruby to do this!
What you are asking for is beyond most of the test tools once you throw in the ability to spider the site.
That final requirement pushes you into the realm of hand-coding something. Using the Mechanize gem you could do all those things, but, you get to code a lot of the navigation of the site.
Mechanize uses Nokogiri internally, so it's easy to grab all links in a page, which you could store in a database to be checked by a different thread, or some subsequent code. That said, writing a spider is not hard if you're the owner of the pages you're hitting, because you can be pretty brutal about accessing the server and let the code run at full speed without worrying about being banned for excessive bandwidth use.

Ways to programatically check if a website is up and functioning as expected

I know this is an open ended question, but hopefully it will get some good answers before the thread is locked...
I'm wondering what methods there are to programmatically check (language agnostic) if a website is online from a client perspective (assume you can't make changes to the site/server, but you can rely on certain behaviours of the site.)
The result of each method could stack to provide a measure of certainty that the site is up/down - that is, a method does not have to provide a definite indication if the site is up/down on its own.
Some common tests just to check 'upness' may be:
Ping the site (which in the case of shared hosting isn't very
indicative)
Send a http head/get request and check the status
Others I can think of to check that the site is up and functioning:
Check you received a well formed html response i.e. html to html
tags, if the site is experiencing trouble it may spit an error and
exit without writing the rest of the page (not all that reliable
though because the site may handle most errors in a better way)
Check certain content is or is not on the page, i.e. perhaps there is some content that is always present on your pages, or always present in the case of an error
Can anybody think of any other methods that could be used to help determine if a site is in fact up/down and functioning/not functioning correctly from within a program?
If your get request on a page that displays info from database comes back with status 200 and matching keywords are found, you can be pretty certain that your site is up and running.
And you don't really need to write your own script to do that. There are free services such as GotSiteMonitor, Pingdom, UptimeRobot etc. allows you to monitor your site.
Based your set of test on the unit tests priciple. It is normally used in programming to test classes, modules or other artefacts after changes have been made. You can use any of the available frameworks, so don't have to reinvent the wheel. You must describe (implement) tests to be run, in your case a typical test should request a url inside the page and then do some evaluations like:
call result (for example return code of curl execution)
http return code
http headers
response mime type
response size
response content (test against a regular expression)
This way you can add, remove and modify single tests without having to care about the framework, once you are up. You can also chain tests, so perform a login in one test and virtually click a button in subsequent test.
There are also tools to handle such test runs automatically including visualization of results, statistics and the like.
OK, it sounds like you want to test and monitor your website from a customer experience perspective rather than purely establishing if a server is up (using ping for example). An effective way to replicate the customer experience is to simulate tests against the site using one of the headless browser testing tools (phantomJS is great a great choice) as they will render the page fully (including images, CSS, JS etc.) giving you a real page load time. These tools also allow you to make assertions on all aspects of the HTML content and HTTP response.
pingdom recently started offering a (paid for) service to perform these exact types of checks for alongside their existing monitoring solution. The demo is worth looking at, their interface for writing the actual tests is very nice.

Does your average web user understand the concept of the clipboard?

I'm designing a web site where I would expect the intended audience to have limited computer skills.
An important part of my site's functionality will require the end user to copy an URL that my site generates and use it in emails, or social network postings, or on their own web site.
I could write the URL to the clipboard when a button is pushed (like tinyurl.com does). However I'm wondering whether the average user even understands what that means and how to use it.
Any guidence will be appreciated.
There are several paths of though possible, you can make many of the scenarios possible at once.
Make the URL copied at once.
Make the URL copied when pushing the button.
Make the URL selected at once so the user can copy it directly
Of course since the URL is copied at once the two other actions does not need to do anything with the clipboard unless the user has modified the clipboard after the URL was presented.
When the URL is copied, at least in the first two steps you could show a message for the user that is has been copied, that makes it useful for those that understand it.
For those that might be focused on copying the URL themselves step 3 helps them on their way.
Whatever path the user tries should go as smooth as possible without forcing them to understand the benefits offered. In the long run it might help them, but if only used a few times the work of understanding what is going on outweighs the benefit.

specific limitations of AJAX?

I'm still pretty new to AJAX and javascript, but I'm getting there slowly.
I have a web-based application that relies heavily on mySQL and there are individual user accounts that are accessed and the UI is populated with user specific data.
I'm working on getting rid of a tabbed navigation bar that currently loads new pages because all that changes from page to page is information within one box.
The thing is that box needs to reload info from the database, etc.
I have had great help from users here showing that I need to call the database within the php page that ajax is calling.
OK-so pardon the lengthy intro-what I'm wondering is are there any specific limitations to what ajax can call that I need to know about? IE: someone mentioned that it's best not to call script files and that I should remove scripts from the php page that is being called and keep those in the 'parent' page. Any other things like this I need to keep in mind?
To clarify: I'm not looking to discuss the merits/drawbacks of the technology. I'm wondering about specific coding implementation that I need to be aware of (for example-I didn't until yesterday realize that if even if I had established a mySQL connection on the page, that I would need to re establish that connection in my called page as well...makes perfect sense now).
XMLHttpRequest which powers ajax has a number of limitations. I recommend brushing up on the same origin policy. This is a pivotal rule because it limits where AJAX calls can be made.
First, you can't have Javascript embedded in the HTTP response to an AJAX call. That's a security issue.
No mention of the dynamics of the database, but if the data to be displayed in tabs doesn't have to be real-time, why not cache it server-side?
I find that like any other protocol, Ajax works best in tightly controlled conditions. It wouldn't make much sense for updating nearly the whole page, unless you find that the user experience is improved with an on-page 'loader'. Without going into workarounds, disadvantages will include losing the browser back button / history, issues such as the one your friend mentioned, and also embedded resources and other rich content can suffer as well, and just having an extra layer of complexity to deal with in your app. Don't treat it as magic sauce for your app - make sure every use delivers specific results that benefit your client / audience.
IMHO, it's best to put your client side javascript in a separate page and then import it - neater container. one thing I've faced before is how to call xml back which contains code to run such as more javascript - it's worth checking if this is likely earlier on and avoiding, than having to look at evals.
Mildly interesting.

GWT, with multiple clients

I am designing a web application using GWT currently, which is also the first time i am using GWT. I just have a general question about how (or can) GWT handles communications between multilpe clients.
My application needs user to login and has personalized pages for different users, GWT is well able to do all of these. The only problem is user needs to know what other users are doing, a simple example is like Google Talk, when one user is "typing", the other side will be noticed. So i am just wondering if GWT can do this?
As i said this is my first time using GWT, so, if GWT is well able to provide the these user interacting functions, i will go with GWT, otherwise i can make changes when it is not too late.
Thanks!!!
Looking at the example you gave, if user A starts typing, there's no problem sending the "started typing" event to the server. The server would than have to look up who user A is talking with (say, user B), and get the information to B's browser. This is, of course, the trickier part, but there is more than one way to perform the task, as described for instance here.
In summary, if you're OK with passing the requests through the server, I don't see a problem with using GWT as the underlying technology.
What you need is server push/ajax push/comet/many other names. I've summarized the options you have for GWT in a different answer.
For a quick start, check out NGiNX_HTTP_Push_Module - IMHO it's the easiest one to customize to your needs and they provide a nice chat example that should get you started. However, if you also use jQuery or Mootools in your application (for example, for UI effects), you might want to also consider Ajax Push Engine/APE-Project (but remember that jQuery/Mootools might require some tweaking to work with GWT). Those two are my favorites :)

Resources