When it is important to write Selenium Code

classic Classic list List threaded Threaded
35 messages Options
12
Reply | Threaded
Open this post in threaded view
|

When it is important to write Selenium Code

Pushpraj Singh
If we have 50 test cases, each has different scenario then what is the point of writing 50 selenium script, one can complete it in lesser time then writing script or How we cover different scenarios just by writing single script.
-Pushprajsingh05@gmail.com
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

softwaretestingforum
Administrator
Hi Pushpraj,

Your one test method should only related validation of a page. In fact one class should encapsulate only the one responsibility.
See this - http://en.wikipedia.org/wiki/Single_responsibility_principle

Now if 50 scenarios are not related to one other then you are better off writing 50 test methods. Else you would bang your head against wall when you see some thing failing and not being able to figure out what went wrong.

Remember one scenario per test. It is good for comprehensibility as well as maintenance.
 
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

Illusion0Reality
In reply to this post by Pushpraj Singh
If there are 10 pages in the application, and there are 30 tests that visit these 10 pages (only) in a different way / with different fields / different values, then only 10 responsibilities (or class files) are sufficient.

So, even if we have 100 scenarios that visit only 10 pages, then 10 test scripts are enough.
The point here is the generic functions should be separated (validating list values, enabled checking, enter text box vlaues etc.).

I have one class for one page, and I use case based (keyword driven) frame work. So, if I have 10 pages (to be interacted with) I wil have only 10 classes.

The test data (or the test flow) will change in the XLS. Based on the XLS data flow, my entire test will run. No need to touch the code to accommodate different test flows - Until unless new fields should be interacted.
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

softwaretestingforum
Administrator
excellent explanation :)

On Thu, Feb 2, 2012 at 5:10 PM, Illusion0Reality [via Manual and Automated Testing] <[hidden email]> wrote:
If there are 10 pages in the application, and there are 30 tests that visit these 10 pages (only) in a different way / with different fields / different values, then only 10 responsibilities (or class files) are sufficient.

So, even if we have 100 scenarios that visit only 10 pages, then 10 test scripts are enough.
The point here is the generic functions should be separated (validating list values, enabled checking, enter text box vlaues etc.).

I have one class for one page, and I use case based (keyword driven) frame work. So, if I have 10 pages (to be interacted with) I wil have only 10 classes.

The test data (or the test flow) will change in the XLS. Based on the XLS data flow, my entire test will run. No need to touch the code to accommodate different test flows - Until unless new fields should be interacted.



If you reply to this email, your message will be added to the discussion below:
http://manual-and-automated-testing.1070.n6.nabble.com/When-it-is-important-to-write-Selenium-Code-tp4355743p4358759.html
To start a new topic under Manual and Automated Testing, email [hidden email]
To unsubscribe from Manual and Automated Testing, click here.
NAML



--
Thanks
Tarun K

~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

Pushpraj Singh
Just copy n paste the Java code(HTML to JUNIT Format) from selenium recorded scneario to eclipse doesnt work, there are few modifications you have to make.
Question: How to make the copy n paste work (atleast 90%) in eclipse, this question is for Selenium-Java beginners?
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

softwaretestingforum
Administrator
Pushpraj Singh wrote
Just copy n paste the Java code(HTML to JUNIT Format) from selenium recorded scneario to eclipse doesnt work, there are few modifications you have to make.
Well just copy/paste will not work, at the least you need to create class, method set up for browser.
If you have already done that then I don't see any reason why copy/paste would not work until unless you are dealing with dynamic id, ajax driven interface, elements which can not be exercised directly in IE.

We could better help you if you specifically mention what you copied/pasted and where you encountered error.  
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

Pushpraj Singh
package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class XYZII964 {
        private WebDriver driver;
        private String baseUrl;
        private StringBuffer verificationErrors = new StringBuffer();
        @Before
        public void setUp() throws Exception {
                driver = new FirefoxDriver();
                baseUrl = "https://staging.xyz.edu/";
                driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        }
Seeing that SELENIUM HAS CREATED one class LMSII964, I  created one class in Myproject>>Mysourcefolder>>Myscript>>XYZII964.java and pasted the above code but eclipse "x" button , red groovy lines below "package com.example.tests",private WebDriver driver;
        private String baseUrl; etc . please help.
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

softwaretestingforum
Administrator
Pushpraj Singh wrote
red groovy lines below "package com.example.tests",private WebDriver driver;
        private String baseUrl; etc . please help.
1. Is your package name - "com.example.tests"?
2. Have you added WebDriver jar to your class path?
3. Looks like you did not import StringBuffer class. In eclipse ctrl + Shift + O will import it for you.


A screen shot of your IDE would have added more information.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

Pushpraj Singh
It worked red lines are gone but there are few orange lines beneath baseurl in above code which gives options on mouse hover like:
-remove baseurl keep side effects assignment
-create getter setter for base url
-Add @supressarnings 'unused' to 'baseurl'
what are these?which option should I choose?
How can I attach s acreeshot to the forum?
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

softwaretestingforum
Administrator
Pushpraj Singh wrote
-Add @supressarnings 'unused' to 'baseurl'
what are these?which option should I choose?
How can I attach s acreeshot to the forum?
As it says 'unused' it means that you just declared baseurl but did not use it any where. If you yet want to keep it then you can use - @supressarnings.

To upload image - user "Insert Image" option in the menu while asking/responding to questions.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

Pushpraj Singh
Thanks for taking me  this far. I have copy pasted and ran the test case sucessfully (without any error) but it should open and in the browser. How can I do that , this is how I would be able to do cross browser testing?
I cant see insert menu while replying new post.
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

softwaretestingforum
Administrator
Pushpraj Singh wrote
I have copy pasted and ran the test case sucessfully (without any error) but it should open and in the browser. How can I do that , this is how I would be able to do cross browser testing?
I cant see insert menu while replying new post.
Well in your sample code you need to have more statement like -

driver.get("URL");
driver.findElement(By.cssSelector("locator")).click()

If you want a sample then look at this -

http://www.seleniumtests.com/2012/01/selenium-2-methods-are-no-more-weird.html?utm_source=BP_recent

About inserting image in post how do you reply to post? Do you reply by mail or come to site and reply? I have attached one image here for your reference. Notice "Insert Image" button in menu.

SampleSnapshot
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

Adrien007
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

softwaretestingforum
Administrator
If you want to run it in IE then just change this line in your code -

Pushpaj Singh wrote
                driver = new FirefoxDriver();
to driver = new InternetExplorerDriver();

About the image attachment. Did you see my previous response how I attached the image and highlighted it?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

Adrien007
I m replying via email, so I can see 'Insert Image' now.
and about my query I am sending Jing Screencast , please have a look where I am missing.
http://screencast.com/t/LfjA4oi68L2e
THANKS
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

softwaretestingforum
Administrator
Buddy, you could have spoken instead of typing in notepad
To answer your question.

Replace your @Before annonation with @BeforeClass(alwaysRun=true). This is TestNG annotation.
Follow this and it should work.

About the test results. There is no Pass. in fact there is no pass, fail or skip. All are zero which mean no execution took place.
 
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

Adrien007
Dont have Mic here ...
Idea of changing Before annotation didn't work
I am sharing another JING video which would clarify my problem in detail manner...
Jing:   http://screencast.com/t/6K74L58Gw3W5

here the code ( IMDB example ) which is working I took from :
http://functionaltestautomation.blogspot.in/2009/10/dataprovider-data-driven-testing-with.html
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

softwaretestingforum
Administrator
I am enjoying this conversation with you

I think I got it.
Notice the @Test annotation in your 'testCrossBrowser' method.
See your import - it refers to junit.Test while you are using TestNG to execute tests hence you should remove junit.Test import and use testng.Test import and it should work.


About those red lines, they are Eclipse indication of fields. Don't worry about them.

~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

Adrien007
1)How to do that :
"Notice the @Test annotation in your 'testCrossBrowser' method.
See your import - it refers to junit.Test while you are using TestNG to execute tests hence you should remove junit.Test import and use testng
."
2)I think code doesn't have code to invoke the browser via selenium server, as we have seen in 'IMDB example'.
Reply | Threaded
Open this post in threaded view
|

Re: When it is important to write Selenium Code

softwaretestingforum
Administrator
Don't worry, you have all the code what is needed to execute test.
See attached image on how you can change the annotation to - import org.testng.annotations.Test;
After this change you should be able to execute test.

Change import from junitTest to testTest
~ seleniumtests.com
12