Guiceberry + junit4+selenium RC

165 views
Skip to first unread message

prabu

unread,
Sep 19, 2010, 3:35:43 AM9/19/10
to guiceberry-users
Could you please give me an example on how to use selenium RC +
guiceberry+junit 4

Thanks
Prabu

Luiz-Otavio Zorzella

unread,
Sep 20, 2010, 1:14:44 PM9/20/10
to guiceber...@googlegroups.com
Prabu,

The tutorial #1 is a good start. See link to the tutorial at
http://code.google.com/p/guiceberry/

In a nutshell,

1. Download the full release source code

http://code.google.com/p/guiceberry/downloads/list

2. Run this

http://code.google.com/p/guiceberry/source/browse/trunk/doc/tutorial/test/junit4/tutorial_1_server/Example0HelloServerTest.java

You've got GuiceBerry + JUnit4 + WebDriver at this point. So:

3. @Inject Selenium selenium

into the test

4. See lines 23 to 27 of this file, where the WebDriver is @Provided:

http://code.google.com/p/guiceberry/source/browse/trunk/doc/tutorial/test/tutorial_1_server/testing/PetStoreEnv0Simple.java

5. Create a similar

@Provides @TestScoped
Selenium getSelenium() {
// put here all the code to get an instance of Selenium -- start the
Selenium RC and all
}

You're done. You might want to start the Selenium RC in the
GuiceBerryEnvMain instead, but even this should work. Post back the
change here when you get it working so it becomes documentation for
others.

Peace,

Z

> --
> You received this message because you are subscribed to the Google Groups "guiceberry-users" group.
> To post to this group, send email to guiceber...@googlegroups.com.
> To unsubscribe from this group, send email to guiceberry-use...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/guiceberry-users?hl=en.
>
>

prabu

unread,
Sep 22, 2010, 2:05:52 AM9/22/10
to guiceberry-users
Luiz,
Thanks for the reply. I configured guiceberry in my selenium RC
+TestNG. It is working. Please let me know if it requires enhancement
or correction

Test class
---------------


package com.catalog.pim;

import static org.testng.AssertJUnit.*;
import com.google.common.testing.TearDown;
import com.google.guiceberry.testng.TestNgGuiceBerry;
import com.google.inject.Inject;
import com.thoughtworks.selenium.Selenium;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import
com.googlecode.seleniumjavaevidence.report.GenerateEvidenceReport;
import com.googlecode.seleniumjavaevidence.selenium.SeleniumEvidence;



import java.lang.reflect.Method;
import java.util.List;


public class CatalogGuiceTestNG {

private TearDown toTearDown;
String exString;

@BeforeMethod
public void setUp(Method m) {
// Make this the call to TestNgGuiceBerry.setUp as early as
possible
toTearDown = TestNgGuiceBerry.setUp(this, m,
ServerEnvSimple.class);
}

@AfterMethod
public void tearDown() throws Exception {
// Make this the call to TestNgGuiceBerry.tearDown as late as
possible
toTearDown.tearDown();
}

@Inject
Selenium selenium;

@Inject
private LoginBot loginBot;



@Test
public void testSuccessfulCase() throws Exception

{


//evidence.
//Logging in
loginBot.login();



//Rest of the testcode
selenium.click("link=Product Editor");


selenium.waitForPageToLoad("60000");
selenium.click("//a[@id='quickSearchBtn']/img");
selenium.waitForPageToLoad("60000");


String pageStr = selenium.getText("//
td[@id='productsGridPagingArea']/table/tbody/tr/td[4]/table/tbody/tr/
td[1]");

long totalRecords =
NumberUtils.toLong(StringUtils.right(pageStr, 2));

System.out.println(" The total number of records is " +
totalRecords);

long numberOfPages = totalRecords / 15;

if (totalRecords % 15 > 0) {
numberOfPages += 1;
}

System.out.println(" The total number of pages is " +
numberOfPages);

for (int i = 1; i <= numberOfPages; i++) {
if (selenium.isElementPresent("//
td[text()='Officejet9120']/preceding-sibling::td[2]/a")) {

System.out.println(" it is in the " + i + "th page");
selenium.click("//td[text()='Officejet9120']/preceding-
sibling::td[2]/a");
break;


} else {
selenium.click("//td[@id='productsGridPagingArea']/
table/tbody/tr/td[5]/table/tbody/tr/td[1]/img");

}


}



}

@Test
public void loginUnsuccessful()
{
loginBot.loginUnsuccessful();
assertTrue(selenium.isTextPresent("Your login attempt was not
successful"));
}


}


Env class
-------------

package com.catalog.pim;

import com.google.guiceberry.GuiceBerryModule;
import com.google.inject.Provides;
import com.googlecode.seleniumjavaevidence.selenium.SeleniumEvidence;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import com.google.guiceberry.TestScoped;

import java.util.ArrayList;
import java.util.List;

public class ServerEnvSimple extends GuiceBerryModule {

@Provides
@TestScoped
Selenium getSelenium() {
Selenium result = new DefaultSelenium(
"localhost",4444,"*firefox","http://xyz.com");
result.start();
return result;
}

@Provides
@TestScoped
List<SeleniumEvidence> getEvidence() {
return new ArrayList<SeleniumEvidence>();
}



}




LoginBot class
---------------------


package com.catalog.pim;

import com.google.inject.Inject;
import com.thoughtworks.selenium.Selenium;


public class LoginBot {


private final Selenium selenium;


@Inject
LoginBot(Selenium selenium)
{
this.selenium = selenium;
}


public void login() {
selenium.open("/");
selenium.click("//a[@href='http://xyz']");
selenium.waitForPageToLoad("60000");
selenium.type("josso_username", "jcadmin");
selenium.type("josso_password", "jcadmin");
selenium.click("//input[@value='Login']");
selenium.waitForPageToLoad("60000");
}

public void loginUnsuccessful() {
selenium.open("/");
selenium.click("//a[@href='xyz']");
selenium.waitForPageToLoad("60000");
selenium.type("josso_username", "jcadmi");
selenium.type("josso_password", "jcadmin");
selenium.click("//input[@value='Login']");
selenium.waitForPageToLoad("60000");

}


}






On Sep 20, 10:14 pm, Luiz-Otavio Zorzella <zorze...@gmail.com> wrote:
> Prabu,
>
> The tutorial #1 is a good start. See link to the tutorial athttp://code.google.com/p/guiceberry/
>
> In a nutshell,
>
> 1. Download the full release source code
>
> http://code.google.com/p/guiceberry/downloads/list
>
> 2. Run this
>
> http://code.google.com/p/guiceberry/source/browse/trunk/doc/tutorial/...
>
> You've got GuiceBerry + JUnit4 + WebDriver at this point. So:
>
> 3. @Inject Selenium selenium
>
> into the test
>
> 4. See lines 23 to 27 of this file, where the WebDriver is @Provided:
>
> http://code.google.com/p/guiceberry/source/browse/trunk/doc/tutorial/...

Luiz-Otavio Zorzella

unread,
Sep 22, 2010, 2:55:22 PM9/22/10
to guiceber...@googlegroups.com
It seems good. The two advices I would give:

1. I would generally prefer creating Selenium as a @Singleton, since
it would be cheaper (sorry I told you @TestScoped originally, it was a
slip of thought, even if it works). Also see note below.

2. Also ideally, the "selenium.start" method should not happen in the
provider method. I don't know if there's a good canonical article out
there on this Guice-y topic, but doing expensive things in a provider
is frowned upon. If you don't "start" in the provider method, this is
where you can move it to:

a. If you make it an @Singleton, put it in the GuiceBerryEnvMain (i.e.
@Inject Selenium into the GBEM, and start it there).

b. If you keep it an @TestScoped, put it in a TestWrapper. BTW, if you
keep it a @TestScoped, you should likely want to "stop" it at the end
of the test, lest selenium instances will pile up. Here's how that
code would look like:

@Provides
TestWrapper getTestWrapper(final TearDownAccepter tearDownAccepter,
final Selenium selenium) {
return new TestWrapper() {
public void toRunBeforeTest() {
tearDownAccepter.addTearDown(new TearDown() {


public void tearDown() throws Exception {

// This gets executed after every test
selenium.stop();
}
});
}
// This gets executed before every test
selenium.start();
}
}

Z

prabu

unread,
Sep 23, 2010, 7:29:55 AM9/23/10
to guiceberry-users
Thanks for the help. I made changes as per your advise.

sudheer babu

unread,
Oct 21, 2013, 6:38:18 AM10/21/13
to guiceber...@googlegroups.com
It was nice and it is very useful for me as well as useful for SELENIUM learners.Thanks for providing this useful information.123 trainings also provides best <a href="http://123trainings.com/it-selenium-online-training.html">SELENIUM Online Training</a>.

sudheer babu

unread,
Oct 21, 2013, 6:38:52 AM10/21/13
to guiceber...@googlegroups.com

sudheer babu

unread,
Oct 24, 2013, 8:30:05 AM10/24/13
to guiceber...@googlegroups.com
Online SAP HANA Training in India by 123 Trainings. We are providing excellent online training in SAP HANA by real-time IT industry experts with unique training methodology and Course Content covers all the in-depth critical scenarios. We have completed more than 200 SAP HANA batches through Online SAP HANA Training program.
Email :123onlineclasses[at]gmail[dot]com
Reply all
Reply to author
Forward
0 new messages