Best FluentLenium code snippet using org.fluentlenium.adapter.cucumber.FluentTestContainer.instance
Source:FluentTestContainer.java
...31 sharedMutator = new ThreadLocal<>();32 injector = new ThreadLocal<>();33 }34 /**35 * Returns single instance of adapter across all Cucumber steps.36 *37 * @return instance of fluent adapter38 */39 public FluentAdapter instance() {40 if (isNull(fluentAdapter.get())) {41 controlContainer.set(new DefaultFluentControlContainer());42 sharedMutator.set(new FluentCucumberSharedMutator());43 if (nonNull(configClass)) {44 fluentAdapter.set(new FluentCucumberTest(controlContainer.get(), configClass, sharedMutator.get()));45 } else {46 fluentAdapter.set(new FluentCucumberTest(controlContainer.get(), sharedMutator.get()));47 }48 injector.set(new FluentInjector(fluentAdapter.get(), null,49 new ComponentsManager(fluentAdapter.get()),50 new DefaultContainerInstantiator(fluentAdapter.get())));51 }52 return fluentAdapter.get();53 }54 /**55 * Reset instance of FluentAdapter stored in container.56 */57 public void reset() {58 sharedMutator.remove();59 controlContainer.remove();60 injector.remove();61 configClass = null;62 fluentAdapter.remove();63 }64 /**65 * Provide control container across different classes.66 *67 * @return control container instance.68 */69 protected FluentControlContainer getControlContainer() {70 if (fluentAdapter.get() == null) {71 instance();72 }73 return controlContainer.get();74 }75 /**76 * Sets config class - needed to enable annotation configuration.77 *78 * @param clazz class annotated with @RunWith(FluentCucumber.class)79 */80 public static void setConfigClass(Class clazz) {81 configClass = clazz;82 }83 /**84 * Returns used inside container SharedMutator85 *86 * @return SharedMutator instance87 */88 protected SharedMutator getSharedMutator() {89 return sharedMutator.get();90 }91 /**92 * Injector used in FluentObjectFactory for creating instances93 *94 * @return fluent injector without loaded full FluentControl context95 */96 public FluentInjector injector() {97 return injector.get();98 }99 /**100 * Creating new instances of pages.101 *102 * @param obj container obj which contains pages to initialize103 */104 public void instantiatePages(Object obj) {105 Arrays.stream(obj.getClass().getDeclaredFields())106 .filter(field -> field.isAnnotationPresent(Page.class))107 .forEach(field -> {108 try {109 Object instance = injector.get().newInstance(field.getType());110 field.setAccessible(true);111 field.set(obj, instance);112 field.setAccessible(false);113 } catch (IllegalAccessException e) {114 e.printStackTrace();115 }116 });117 }118}...
Source:FluentObjectFactory.java
...10/**11 * It is an object factory for creating Cucumber steps objects in FluentLenium injection container12 */13public class FluentObjectFactory implements ObjectFactory {14 private final Map<Class<?>, Object> instances = new HashMap<>();15 private Class<?> configClass;16 @Override17 public void start() {18 if (nonNull(configClass)) {19 setConfigClass(configClass);20 FLUENT_TEST.instance();21 } else {22 setConfigClass(null);23 FLUENT_TEST.instance();24 }25 }26 @Override27 public void stop() {28 FLUENT_TEST.reset();29 this.instances.clear();30 }31 @Override32 public boolean addClass(Class<?> aClass) {33 if (configClass == null) {34 configClass = checkClassForConfiguration(aClass);35 if (nonNull(configClass)) {36 setConfigClass(configClass);37 }38 }39 return true;40 }41 @Override42 public <T> T getInstance(Class<T> type) { // NOPMD43 try {44 T instance = type.cast(instances.get(type));45 if (instance == null) {46 instance = cacheNewInstance(type);47 }48 return instance;49 } catch (Exception e) {50 throw new CucumberException(String.format("Failed to instantiate %s", type), e);51 }52 }53 private <T> T cacheNewInstance(Class<T> type) {54 try {55 T instance = FLUENT_TEST.injector().newInstance(type);56 FLUENT_TEST.injector().inject(instance);57 instances.put(type, instance);58 return instance;59 } catch (Exception e) {60 throw new CucumberException(String.format("Failed to instantiate %s", type), e);61 }62 }63 private Class<?> checkClassForConfiguration(Class<?> cls) {64 Class superClass = cls.getSuperclass();65 if (superClass != null && superClass.isAnnotationPresent(FluentConfiguration.class)) {66 return superClass;67 } else if (cls.isAnnotationPresent(FluentConfiguration.class)) {68 return cls;69 } else {70 return null;71 }72 }...
Source:FluentTestContainerTest.java
...12 FLUENT_TEST.reset();13 }14 @Test15 public void shouldGetFluentCucumberTestInstance() {16 FluentAdapter test = FLUENT_TEST.instance();17 assertThat(test)18 .isInstanceOf(FluentCucumberTest.class)19 .isNotNull();20 }21 @Test22 public void shouldAlwaysReturnTheSameFluentTestInstance() {23 FluentAdapter test = FLUENT_TEST.instance();24 FluentAdapter test2 = FLUENT_TEST.instance();25 assertThat(test)26 .isNotNull()27 .isEqualTo(test2);28 }29 @Test30 public void childClassShouldProvideSameControlContainer() {31 FLUENT_TEST.instance();32 ExampleTest1 test1 = new ExampleTest1();33 ExampleTest2 test2 = new ExampleTest2();34 assertThat(test1.returnContainer())35 .isNotNull()36 .isEqualTo(test2.returnContainer());37 }38 private class ExampleTest1 extends FluentCucumberTest {39 public FluentControlContainer returnContainer() {40 return this.getControlContainer();41 }42 }43 private class ExampleTest2 extends FluentCucumberTest {44 public FluentControlContainer returnContainer() {45 return this.getControlContainer();...
instance
Using AI Code Generation
1import cucumber.api.java.en.Given;2import cucumber.api.java.en.Then;3import cucumber.api.java.en.When;4import org.fluentlenium.adapter.cucumber.FluentTestContainer;5public class MyStepdefs extends FluentTestContainer {6 @Given("^I am on the Google search page$")7 public void iAmOnTheGoogleSearchPage() throws Throwable {8 }9 @When("^I search for \"([^\"]*)\"$")10 public void iSearchFor(String arg0) throws Throwable {11 $("#lst-ib").fill().with(arg0);12 $("#lst-ib").submit();13 }14 @Then("^the page title should start with \"([^\"]*)\"$")15 public void thePageTitleShouldStartWith(String arg0) throws Throwable {16 assert(title().startsWith(arg0));17 }18}19import cucumber.api.java.en.Given;20import cucumber.api.java.en.Then;21import cucumber.api.java.en.When;22import org.fluentlenium.adapter.cucumber.FluentCucumberTest;23public class MyStepdefs extends FluentCucumberTest {24 @Given("^I am on the Google search page$")25 public void iAmOnTheGoogleSearchPage() throws Throwable {26 }27 @When("^I search for \"([^\"]*)\"$")28 public void iSearchFor(String arg0) throws Throwable {29 $("#lst-ib").fill().with(arg0);30 $("#lst-ib").submit();31 }32 @Then("^the page title should start with \"([^\"]*)\"$")33 public void thePageTitleShouldStartWith(String arg0) throws Throwable {34 assert(title().startsWith(arg0));35 }36}37import cucumber.api.java.en.Given;38import cucumber.api.java.en.Then;39import cucumber.api.java.en.When;40import org.fluentlenium.adapter.cucumber.FluentCucumberTest;41public class MyStepdefs extends FluentCucumberTest {42 @Given("^I am on the Google search page$")43 public void iAmOnTheGoogleSearchPage() throws Throwable {44 }
instance
Using AI Code Generation
1package com.test;2import cucumber.api.java.en.Given;3import cucumber.api.java.en.Then;4import cucumber.api.java.en.When;5import org.fluentlenium.adapter.cucumber.FluentTestContainer;6import org.fluentlenium.adapter.cucumber.FluentTestContainer$;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.chrome.ChromeDriver;9public class StepDefs extends FluentTestContainer {10 @Given("^I am on the Google search page$")11 public void i_am_on_the_Google_search_page() throws Throwable {12 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Selenium\\chromedriver.exe");13 WebDriver driver = new ChromeDriver();14 FluentTestContainer$.MODULE$.initFluent(driver);15 }16 @When("^I search for \"([^\"]*)\"$")17 public void i_search_for(String arg1) throws Throwable {18 fill("#lst-ib").with(arg1);19 submit("#lst-ib");20 }21 @Then("^the page title should start with \"([^\"]*)\"$")22 public void the_page_title_should_start_with(String arg1) throws Throwable {23 assert(title().startsWith(arg1));24 }25}26package com.test;27import cucumber.api.java.en.Given;28import cucumber.api.java.en.Then;29import cucumber.api.java.en.When;30import org.fluentlenium.adapter.cucumber.FluentTestContainer;31import org.fluentlenium.adapter.cucumber.FluentTestContainer$;32import org.openqa.selenium.WebDriver;33import org.openqa.selenium.chrome.ChromeDriver;34public class StepDefs extends FluentTestContainer {35 @Given("^I am on the Google search page$")36 public void i_am_on_the_Google_search_page() throws Throwable {37 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Selenium\\chromedriver.exe");38 WebDriver driver = new ChromeDriver();39 FluentTestContainer$.MODULE$.initFluent(driver);40 }41 @When("^I search for \"([^\"]*)\"$")42 public void i_search_for(String arg1) throws Throwable {43 fill("#lst-ib").with(arg1);44 submit("#lst-ib");45 }46 @Then("^the page title should start with \"([^\"]*)\"$")
instance
Using AI Code Generation
1package stepdefs;2import cucumber.api.java.en.Given;3import cucumber.api.java.en.Then;4import cucumber.api.java.en.When;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.chrome.ChromeDriver;7public class Stepdefs extends org.fluentlenium.adapter.cucumber.FluentTestContainer {8 @Given("^I am on the Google search page$")9 public void I_visit_google() {10 WebDriver driver = new ChromeDriver();11 initFluent(driver);12 initTest();13 }14 @When("^I search for \"([^\"]*)\"$")15 public void search_for(String query) {16 fill("#lst-ib").with(query);17 submit("#lst-ib");18 }19 @Then("^the page title should start with \"([^\"]*)\"$")20 public void checkTitle(String title) {21 await().atMost(5000).until(".srg").present();22 assert title().startsWith(title);23 }24}25package stepdefs;26import cucumber.api.java.en.Given;27import cucumber.api.java.en.Then;28import cucumber.api.java.en.When;29import org.openqa.selenium.WebDriver;30import org.openqa.selenium.chrome.ChromeDriver;31public class Stepdefs extends org.fluentlenium.adapter.cucumber.FluentTestContainer {32 @Given("^I am on the Google search page$")33 public void I_visit_google() {34 WebDriver driver = new ChromeDriver();35 initFluent(driver);36 initTest();37 }38 @When("^I search for \"([^\"]*)\"$")39 public void search_for(String query) {40 fill("#lst-ib").with(query);41 submit("#lst-ib");42 }43 @Then("^the page title should start with \"([^\"]*)\"$")44 public void checkTitle(String title) {45 await().atMost(5000).until(".srg").present();46 assert title().startsWith(title);47 }48}49package stepdefs;50import cucumber.api.java.en.Given;51import cucumber.api.java.en.Then;52import cucumber.api.java.en.When;53import org.openqa.selenium.WebDriver;54import org.openqa.selenium.chrome.ChromeDriver;
instance
Using AI Code Generation
1package org.fluentlenium.adapter.cucumber.integration.tests;2import cucumber.api.java.en.Given;3import cucumber.api.java.en.Then;4import cucumber.api.java.en.When;5import org.fluentlenium.adapter.cucumber.integration.pages.local.LocalPage;6import org.fluentlenium.adapter.cucumber.integration.pages.local.LocalPageWithUrl;7import org.fluentlenium.adapter.cucumber.integration.pages.local.LocalPageWithUrlAndPath;8import org.openqa.selenium.WebDriver;9import static org.assertj.core.api.Assertions.assertThat;10public class LocalSteps extends FluentTestContainer {11 private WebDriver driver;12 @Given("^I am on the local test page$")13 public void iAmOnTheLocalTestPage() {14 driver = getDriver();15 goTo(LocalPage.class);16 }17 @When("^I go to the local test page with URL$")18 public void iGoToTheLocalTestPageWithUrl() {19 goTo(LocalPageWithUrl.class);20 }21 @When("^I go to the local test page with URL and path$")22 public void iGoToTheLocalTestPageWithUrlAndPath() {23 goTo(LocalPageWithUrlAndPath.class);24 }25 @Then("^I should see the local test page$")26 public void iShouldSeeTheLocalTestPage() {27 }28 @Then("^I should see the local test page with URL$")29 public void iShouldSeeTheLocalTestPageWithUrl() {30 }31 @Then("^I should see the local test page with URL and path$")32 public void iShouldSeeTheLocalTestPageWithUrlAndPath() {33 }34}35package org.fluentlenium.adapter.cucumber.integration.tests;36import cucumber.api.java.en.Given;37import cucumber.api.java.en.Then;38import cucumber.api.java.en.When;39import org.fluentlenium.adapter.cucumber.integration.pages.local.LocalPage;40import org.fluentlenium.adapter.cucumber.integration.pages.local.LocalPageWithUrl
instance
Using AI Code Generation
1public class MyStepdefs {2 @Given("^I am on the Google search page$")3 public void iAmOnTheGoogleSearchPage() {4 }5}6public class MyStepdefs {7 @Given("^I am on the Google search page$")8 public void iAmOnTheGoogleSearchPage() {9 $(By.name("q")).fill().with("FluentLenium");10 $(By.name("btnG")).submit();11 }12}13public class MyStepdefs {14 @Given("^I am on the Google search page$")15 public void iAmOnTheGoogleSearchPage() {16 $(By.name("q")).fill().with("FluentLenium");17 $(By.name("btnG")).submit();18 }19}20public class MyStepdefs {21 @Given("^I am on the Google search page$")22 public void iAmOnTheGoogleSearchPage() {23 $(By.name("q")).fill().with("FluentLenium");24 $(By.name("btnG")).submit();25 }26}27public class MyStepdefs {28 @Given("^I am on the Google search page$")29 public void iAmOnTheGoogleSearchPage() {30 $(By.name("q")).fill().with("FluentLenium");31 $(By.name("btnG")).submit();32 }33}34public class MyStepdefs {35 @Given("^I am on the Google search page$")36 public void iAmOnTheGoogleSearchPage() {37 $(By.name("q")).fill
instance
Using AI Code Generation
1package org.fluentlenium.adapter.cucumber;2import cucumber.api.java.en.Then;3import cucumber.api.java.en.When;4import org.fluentlenium.adapter.cucumber.FluentTestContainer;5import org.fluentlenium.core.annotation.Page;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.htmlunit.HtmlUnitDriver;8import org.openqa.selenium.support.ui.WebDriverWait;9import org.testng.Assert;10public class StepDef {11 private PageObject pageObject;12 @When("^I go to the Google page$")13 public void I_go_to_the_Google_page() throws Throwable {14 pageObject.goTo();15 }16 @Then("^the title should be \"([^\"]*)\"$")17 public void the_title_should_be(String title) throws Throwable {18 Assert.assertEquals(pageObject.getTitle(), title);19 }20 @Then("^I should see \"([^\"]*)\"$")21 public void i_should_see(String text) throws Throwable {22 Assert.assertTrue(pageObject.containsText(text));23 }24 @When("^I search for \"([^\"]*)\"$")25 public void i_search_for(String text) throws Throwable {26 pageObject.fillSearchField(text);27 }28 @Then("^I should see \"([^\"]*)\" in the title$")29 public void i_should_see_in_the_title(String text) throws Throwable {30 Assert.assertTrue(pageObject.titleContainsText(text));31 }32 @Then("^I should see \"([^\"]*)\" in the result$")33 public void i_should_see_in_the_result(String text) throws Throwable {34 Assert.assertTrue(pageObject.resultContainsText(text));35 }36}37package org.fluentlenium.adapter.cucumber;38import cucumber.api.java.en.Then;39import cucumber.api.java.en.When;40import org.fluentlenium.adapter.cucumber.FluentTestContainer
instance
Using AI Code Generation
1package com.cucumber.framework.stepdefinition;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4import org.openqa.selenium.firefox.FirefoxDriver;5import com.cucumber.framework.helper.Logger.LoggerHelper;6import com.cucumber.framework.helper.TestBase.TestBase;7import com.cucumber.framework.helper.Wait.WaitHelper;8import cucumber.api.Scenario;9import cucumber.api.java.After;10import cucumber.api.java.Before;11public class ServiceHooks extends TestBase{12 private LoggerHelper log = LoggerHelper.getLogger(ServiceHooks.class);13 public void initializeTest() {14 Scenario scn = FluentTestContainer.getInstance().getCurrentScenario();15 log.info("Scenario : "+scn.getName());16 log.info("Scenario ID : "+scn.getId());17 log.info("Scenario Status : "+scn.getStatus());18 log.info("Scenario Line Number : "+scn.getLine());19 log.info("Scenario URI : "+scn.getUri());20 log.info("Scenario Source Tag Names : "+scn.getSourceTagNames());21 log.info("Scenario Tag Names : "+scn.getSourceTagNames());22 log.info("Scenario is Failed : "+scn.isFailed());23 }24 public void embedScreenshot(Scenario scenario) {25 if(scenario.isFailed()) {26 try {27 scenario.write("Current Page URL is " + driver.getCurrentUrl());28 byte[] screenshot = getScreenshotAs(OutputType.BYTES);29 scenario.embed(screenshot, "image/png");30 } catch (Exception e) {31 e.printStackTrace();32 }33 }34 driver.quit();35 }36}37package com.cucumber.framework.stepdefinition;38import org.openqa.selenium.WebDriver;39import org.openqa.selenium.chrome.ChromeDriver;40import org.openqa.selenium.firefox.FirefoxDriver;41import com.cucumber.framework.helper.Logger.LoggerHelper;42import com.cucumber.framework.helper.TestBase.TestBase;43import com.cucumber.framework.helper.Wait.WaitHelper;44import cucumber.api.Scenario;45import cucumber.api.java.After;46import cucumber.api.java.Before;47public class ServiceHooks extends TestBase{48 private LoggerHelper log = LoggerHelper.getLogger(ServiceHooks.class);49 public void initializeTest() {50 Scenario scn = FluentTestContainer.getInstance().getCurrentScenario();51 log.info("Scenario
instance
Using AI Code Generation
1package org.fluentlenium.adapter.cucumber;2import cucumber.api.java.en.Given;3import cucumber.api.java.en.Then;4import cucumber.api.java.en.When;5import org.fluentlenium.adapter.FluentTest;6import org.fluentlenium.core.annotation.Page;7import static org.fluentlenium.core.filter.FilterConstructor.withText;8import static org.junit.Assert.assertEquals;9public class Stepdefs {10 private GooglePage googlePage;11 @Given("^I am on the Google search page$")12 public void I_visit_google() {13 googlePage.go();14 }15 @When("^I search for \"([^\"]*)\"$")16 public void I_search_for(String term) {17 googlePage.fillSearch(term).submitSearch();18 }19 @Then("^the page title should start with \"([^\"]*)\"$")20 public void the_page_title_should_start_with(String title) {21 assertEquals(title, googlePage.getTitle());22 }23 @Then("^the page should contain \"([^\"]*)\"$")24 public void the_page_should_contain(String text) {25 googlePage.hasContent(withText(text));26 }27}28package org.fluentlenium.adapter.cucumber;29import org.fluentlenium.core.FluentPage;30import org.openqa.selenium.support.FindBy;31import org.openqa.selenium.support.How;32import org.openqa.selenium.WebElement;33public class GooglePage extends FluentPage {34 @FindBy(how = How.NAME, using = "q")35 private WebElement searchInput;36 @FindBy(how = How.NAME, using = "btnG")37 private WebElement searchButton;38 public void fillSearch(String text) {39 searchInput.sendKeys(text);40 }41 public void submitSearch() {42 searchButton.submit();43 }44 public String getUrl() {45 }46 public void isAt() {47 assertThat(searchInput).isDisplayed();48 }49}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!