Fetch all Form tags from HTML from console in eclipse selenium java - eclipse

I got the HTML in console using getpagesource in selenium java .
Now i need only the tag 'Forms' in the console result.
How do i do that?
public class Test {
private static final String HTMLPageSourceCode = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Selenium project\\chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://''/tandem/login/?");
String pagesource = driver.getPageSource();
System.out.println(pagesource);
}

You can use following code snippet for this :
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//Sample Java class to fetch list of all tag with tagname forms
public class Test {
public static void main(String[] args) {
String geckoDriverPath=Class1.class.getResource("/firefox/geckodriver.exe").getPath();
System.setProperty("webdriver.gecko.driver",geckoDriverPath);
WebDriver driver = new FirefoxDriver();
driver.get("http://www.URL.com");
List<WebElement>list =driver.findElements(By.tagName("form"));
for(WebElement ele:list) {
System.out.println(ele.getText());
}
}
}
Once you get the lists you can fetch individual form element and perform action like getting text.Hope, this helps

Related

Spring Cloud Contract Stub Runner : how to configure Wiremock server?

package com.example.stubrunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.contract.stubrunner.server.EnableStubRunnerServer;
import org.springframework.cloud.contract.wiremock.WireMockConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
#SpringBootApplication
#EnableStubRunnerServer
public class StubRunnerApplication {
public static void main(String[] args) {
SpringApplication.run(StubRunnerApplication.class, args);
}
#Bean
public WireMockConfigurationCustomizer optionsCustomizer() {
WireMockConfigurationCustomizer customizer = new WireMockConfigurationCustomizer() {
#Override
public void customize(com.github.tomakehurst.wiremock.core.WireMockConfiguration config) {
config.jettyHeaderBufferSize(16384);
}
};
return customizer;
}
}
Above customizer bean does not seem to have any effect. This feature has not much documentation. With security token headers Wiremock's (jettty) default value is just too little.
I used start.spring.io with (current) defaults: spring boot 2.5.5. and spring cloud Hoxton.SR3.
java -jar wiremock-standalone-2.26.3.jar --jetty-header-buffer-size 16384
works just fine.
EDIT :
package com.example.wiremockrunnerlatest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.contract.stubrunner.server.EnableStubRunnerServer;
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner;
#SpringBootApplication
#EnableStubRunnerServer
#AutoConfigureStubRunner(httpServerStubConfigurer = HeaderSizeConfigurer.class)
public class WiremockRunnerLatestApplication {
public static void main(String[] args) {
SpringApplication.run(WiremockRunnerLatestApplication.class, args);
}
}
... and then :
public class HeaderSizeConfigurer extends WireMockHttpServerStubConfigurer {
#Override
public WireMockConfiguration configure(WireMockConfiguration httpStubConfiguration, HttpServerStubConfiguration httpServerStubConfiguration) {
return httpStubConfiguration.jettyHeaderBufferSize(16384);
}
}
Have you tried using #AutoConfigureStubRunner annotation?
Just add below annotation in your tests:
#AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.CLASSPATH,
ids = "com.org:servicename:+:stubs")
Here stubsmode is classpath which means that stubs would be available in classpath.
to do that add:
testCompile("com.org:servicename:+:stubs") { transitive = false }
in your build if your using gradle or add equivalient from maven.
This will download the application automatically from remote and configures the wiremock server for stubs to be available.
complete configuration to run a spring boot test is like:
#RunWith(SpringRunner.class)
#SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.MOCK,
classes = HiltiIntegrationApplication.class)
#AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.CLASSPATH,
ids = "com.ict:organization-management:+:stubs")
#DirtiesContext
Hope this helps!

want to call webelements declared inside a method with args in one class and wanted to call the webele method

package First;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class First1 {
public static WebDriver driver=new FirefoxDriver();
public static WebElement webele;
public static void LaunchApp(String url){
driver.get(url);
}
public static String webele(String loc){
webele= driver.findElement(By.id(loc));
System.out.println("abc");
}
}
package First;
public class First2 {
public static void main(String[] args) {
First1.LaunchApp("https://facebook.com").sendkeys("hdkjasj");
}
}
Here is my next class in the same package to call the method webele with arguments. when trying to call the first class in the second I am getting error as cannot invoke sendkeys(String) into primitive Type void.

Error: Main method not found in class newpackage.Myclass, please define the main method as: public static void main(String[] args)

Trace:
Error: Main method not found in class newpackage.Myclass, please
define the main methods: public static void main(String[] args) or a
JavaFX application class must extend javafx.application.Application.
MyClass.java
package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import javafx.application.*;
public class Myclass {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Users \\karokiaswamy\\Documents\\Selenium_installation\\geckodriver-v0.15.0-win64)\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://Application URL");
driver.findElement(By.xpath("//*[#id='siteLoginTab']/form/table/tbody/tr[1]/td[1]/input")).sendKeys("USER NAME");
driver.findElement(By.xpath("//*[#id='siteLoginTab']/form/table/tbody/tr[1]/td[2]/input")).sendKeys("Password");
driver.findElement(By.xpath("//*[#id='siteLoginTab]/form/table/tbody/tr[1]/td[3]/input")).click();
driver.findElement(By.xpath(".//*[#id='siteLoginTab']/form/table/tbody/tr[1]/td[2]/input")).sendKeys(" ");
driver.findElement(By.xpath(".//*[#id='siteMain']/div[1]/div/form/table/tbody/tr[3]/td/input")).click();
driver.findElement(By.xpath(".//*[#id='siteMain']/div[1]/div/form/table/tbody/tr[3]/td/input")).click();
}
}

Using Testng in Eclipse Juno

I am using Eclipse Juno. I installed TestNG plugins properly. But when I want to run test I can't find a TestNG option in Run As. What's the matter?
My Class is
package com.oasisdigital.rental.client;
import static javax.ws.rs.core.Response.Status.CREATED;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import javax.ws.rs.core.Response;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.oasisdigital.rental.test.AbstractIT;
#Test
public class ClientResourceIT extends AbstractIT {
private ClientApi clientApi;
#Override
#BeforeMethod
public void setUp() {
super.setUp();
this.clientApi = new ClientApi(api);
}
#Test
public void shouldReturnEmptyListWhenNoProviders() {
assertThat(clientApi.getClients(), is(empty()));
}
#Test
public void shouldReturnClientAfterCreation() {
Response resp = clientApi.postClient("Jimmy");
assertThat(resp.getStatus(), is(CREATED.getStatusCode()));
ClientDto client = resp.readEntity(ClientDto.class);
assertThat(client.getId(), is(notNullValue()));
assertThat(client.getName(), is("Jimmy"));
assertThat(clientApi.getClients(), contains(client));
}
}

Why I receive error when use Cover As in Eclipse and not with Junit?

I use Eclipse with eCobertura
I have a little project with a Controller (SpringMVC).
I created a test (JUnit).
When I run the test from JUnit (in Eclipse IDE) all is right but when I run the command (from menu) I receive an error
My controller :
package ec.europa.eu.nwi.web.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* #author LEBRUJA
*/
#Controller
public class AvailibilityController {
/**
* #param request
* #return mav
*/
#RequestMapping(value = "/available")
public final ModelAndView available(final HttpServletRequest request) {
final ModelAndView mav = new ModelAndView("available", "sample",
new String("availability on 0.0.1"));
return mav;
}
}
My Test :
package ec.europa.eu.nwi.web.controller.test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.ModelAndViewAssert;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import ec.europa.eu.nwi.web.controller.AvailibilityController;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:spring-servlet.xml"})
public final class AvalibilityControllerTest {
private transient MockHttpServletRequest request;
private transient MockHttpServletResponse response;
#Autowired
private RequestMappingHandlerAdapter handlerAdapter;
#Autowired
private RequestMappingHandlerMapping handlerMapping;
private static final Logger LOGGER = LoggerFactory.getLogger(AvalibilityControllerTest.class);
#Before
public void setUp() throws Exception {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}
#After
public void tearDown() throws Exception {
LOGGER.debug("TearDown");
}
#Test
public void testAvailable() {
LOGGER.debug("Start testAvailable1");
LOGGER.debug("Test only availibility of the apps");
final AvailibilityController avc = new AvailibilityController();
final Object mav = avc.available(request);
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(mav instanceof ModelAndView);
ModelAndViewAssert.assertAndReturnModelAttributeOfType((ModelAndView)mav, "sample", String.class);
ModelAndViewAssert.assertModelAttributeAvailable((ModelAndView)mav, "sample");
ModelAndViewAssert.assertModelAttributeValue((ModelAndView)mav, "sample", "availability on 0.0.1");
ModelAndViewAssert.assertViewName((ModelAndView)mav, "available");
final BindingResult result = mock(BindingResult.class);
when(result.hasErrors()).thenReturn(true);
LOGGER.debug("End testAvailable1");
}
#Test
public void testAvailable1() throws Exception {
LOGGER.debug("Start testAvailable");
LOGGER.debug("Test only availibility of the apps");
request.setMethod("GET");
request.setRequestURI("/available.html");
Object handler = handlerMapping.getHandler(request).getHandler();
LOGGER.debug("Get the Model and View");
ModelAndView modelAndView = handlerAdapter.handle(request, response,handler);
Assert.assertEquals("availability on 0.0.1", modelAndView.getModel().get("sample"));
Assert.assertTrue(modelAndView.getModel().containsKey("sample"));
LOGGER.debug("End testAvailable");
}
}
If I run with JUnit (Run As Junit), all is right but when I run Cover As .. JUnit I receive the error.
The error :
I filtered the class (from exclude configuration in Eclipse Coverage Configuration).
If I removed the filter, the junit code is marked in red
I don't understand the error.
Thanks a lot
I had
<aop:aspectj-autoproxy proxy-target-class="true" />
In my applicationContext.xml and it is running fine now
Thanks a lot