How to configure Selenide remote grid url in Selenium-Jupiter test framework? - remotewebdriver

I am trying to configure a Selenide driver within the Selenium-Jupiter framework, to use my remote grid url but it keeps ignoring the configuration, and just runs the local installed browser. Here is how I am trying to configure it. Any idea what might be wrong here?
import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.SelenideConfig;
import io.github.bonigarcia.seljup.SelenideConfiguration;
import static com.codeborne.selenide.Browsers.CHROME;
public abstract class ChromeTest extends BaseTest {
#SelenideConfiguration
SelenideConfig selenideConfig = new SelenideConfig();
private String getSeleniumRemote() {
System.getProperty("selenide.remote", "");
}
public ChromeTest() {
if (getSelenideRemote().isEmpty()) {
selenideConfig.proxyEnabled(false)
.browser(CHROME).startMaximized(false)
.browserSize("800x1200").browserPosition("50x60");
} else {
Configuration.timeout = 6000;
Configuration.remote = getSelenideRemote();
selenideConfig.proxyEnabled(false)
.startMaximized(true).browser(CHROME);
}
}
}
I know the regular RemoteWebDriver works and I can get that working but I am hoping to use Selenide in the above example:
Example:
#Test
void testWithRemoteSelenide(#DriverUrl("http://127.1:4444/wd/hub")
#DriverCapabilities("browserName=" + CHROME) SelenideDriver driver)
I can get it to work with the annotation, but the problem is that I need that annotation to be conditional on passing a param to the tests. I want to be able to easily switch using grid or local. Thanks for your help anyone.

Ok, after almost 48 hours an no reponse, I finally figured out the solution. Here it is:
//build.gradle
test {
useJUnitPlatform()
ignoreFailures = false
beforeTest { descriptor ->
logger.lifecycle("Running test: $descriptor.className")
}
systemProperty "env", System.getProperty("env")
def remote = System.getProperty("selenide.remote", "")
if (!remote.isEmpty()) {
systemProperty("selenide.remote", remote)
}
}
Then, in my test base class:
public abstract class ChromeTest extends BaseTest {
#SelenideConfiguration
SelenideConfig selenideConfig = new SelenideConfig();
/**
* This config is equivilant to the documented method:
* Example: test(#DriverUrl("http://127.1:4444/wd/hub")
* #DriverCapabilities("browserName=chrome") SelenideDriver sd)
*/
public ChromeFormTest() {
if (getSelenideRemote().isEmpty()) {
selenideConfig.proxyEnabled(false).proxyHost("http://proxy.domain.com")
.proxyPort(8080)
.browser(CHROME).startMaximized(false)
.browserSize("800x1200").browserPosition("50x60");
} else {
Configuration.timeout = 6000;
Configuration.remote = getSelenideRemote();
selenideConfig.proxyEnabled(false).proxyHost("http://proxy.domain.com")
.proxyPort(8080)
.startMaximized(false).browser(CHROME);
}
}
}
Then, when I execute, it looks like this:
gradle clean test -Denv=sys -Dselenide.remote=http://127.1:4444/wd/hub
--info --tests com.qa.suite.*
And the constructor of each test looks like:
#Test
public void testWhatever(SelenideDriver sd) {

Related

Quarkus #CacheResult is not working properly

I am trying to use quarkus-cache by following the appropriate quarkus doc. I have the below code setup
#ApplicationScoped
class MyClass {
public result doSomething() {
String remoteData = getRemoteData(url);
}
#CacheResult(cacheName = "myCacheName")
public String getRemoteData(String url) {
return remoteCall(url);
}
}
Usage
// Grpc impl class
// call to Myclass.doSomething()
Execution is not proceeding further when getRemoteData() is called the first time. Also, not getting any error.
Am I missing something?

Spring Boot Cucumber Java 8 Testing ApplicationOnReady Event

I have a reporting application that generates a report on ApplicationReadyEvent. I am trying to write cucumber tests for it but as the application event is fired even before my feature is executed , i am not sure what is the right way to test it. Can i control the event during testing ?
#EventListener(ApplicationReadyEvent.class)
private void generateAccuracyAnalysisReport() throws IOException
{
//some Logic
}
Cucumber Classes :
#SpringBootTest
#CucumberContextConfiguration
#ActiveProfiles("junit")
public class CucumberConfiguration
{
}
#RunWith(Cucumber.class)
#CucumberOptions(plugin = "pretty", features = "src/test/resources/cucumber/features")
public class CucumberFullIntegrationTest
{
}
Step Definition:
public class ReportStepDefs implements En {
public ReportStepDefs() {
When("^System sends an application event to generate report$", () -> {
});
Then("^Report should be generated successfully\\.$", () -> {
});
}
}
If your Cucumber tests involve Spring life-cycle you can not use cucumber-spring. Rather you have to use something like Springs ApplicationContextRunner to, configure, run and verify something about your application as part of each scenario.
// Given
ApplicationContextRunner contextRunner = new ApplicationContextRunner();
// When
contextRunner.withConfiguration(AutoConfigurations.of(...);
// Then
contextRunner.run(context -> assertThat(context).... /* something */ );
// Or assert something external to the application context.
Though it sounds like your application is doing something once and then exits. If so you should be using the CommandLineRunner instead of ApplicationReadyEvent in a web application. This is testable with cucumber-spring.
#RequiredArsConstructor
public class StepDefinitions {
final MyCommandLineRunner commandLineRunner;
#When(....)
public void something() {
commandLineRunner.run("input.txt", "input2.txt");
}
#Then(....)
public void assertSomething() {
// check if report was generated
}
}

//NonCompliant comment usage - SonarQube Custom Rule

I am trying to write a few SONARQUBE custom rules for my project.
After reading up the below document -
https://docs.sonarqube.org/display/PLUG/Writing+Custom+Java+Rules+101
and
https://github.com/SonarSource/sonar-custom-rules-examples,
I created a custom rule like these classes below -
The Rule file:
#Rule(key = "MyAssertionRule")
public class FirstSonarCustomRule extends BaseTreeVisitor implements JavaFileScanner {
private static final String DEFAULT_VALUE = "Inject";
private JavaFileScannerContext context;
/**
* Name of the annotation to avoid. Value can be set by users in Quality
* profiles. The key
*/
#RuleProperty(defaultValue = DEFAULT_VALUE, description = "Name of the annotation to avoid, without the prefix #, for instance 'Override'")
protected String name;
#Override
public void scanFile(JavaFileScannerContext context) {
this.context = context;
System.out.println(PrinterVisitor.print(context.getTree()));
scan(context.getTree());
}
#Override
public void visitMethod(MethodTree tree) {
List<StatementTree> statements = tree.block().body();
for (StatementTree statement : statements) {
System.out.println("KIND IS " + statement.kind());
if (statement.is(Kind.EXPRESSION_STATEMENT)) {
if (statement.firstToken().text().equals("Assert")) {
System.out.println("ERROR");
}
}
}
}
}
The Test class:
public class FirstSonarCustomRuleTest {
#Test
public void verify() {
FirstSonarCustomRule f = new FirstSonarCustomRule();
f.name = "ASSERTION";
JavaCheckVerifier.verify("src/test/files/FirstSonarCustom.java", f);
}
}
And finally - the Test file:
class FirstSonarCustom {
int aField;
public void methodToUseTestNgAssertions() {
Assert.assertTrue(true);
}
}
The above Test file would later be my Project's source code.
As per the SONAR documentation - the // Noncompliant is a mandatory comment in my Test file. Thus my first question is should I add this comment everywhere in my Source code too?
If yes - is there any way I can avoid adding this comment, because I do not want to add that code refactoring exercise all over.
Can someone suggest me what I need to do here?
I am using SONARQUBE 6.3.
This comment is only used by the test framework (JavaCheckVerifier class) to test the implementation of your rule. It is not mandatory in any way and for sure you don't need it in your real code.

Sonarqube PostProjectAnalysisTask example?

I have been searching for any PostProjectAnalysisTask working code example, with no look. This page states that HipChat plugin uses this hook, but it seems to me that it still uses the legacy PostJob extension point ...
There is an example on their page now.
https://docs.sonarqube.org/display/DEV/Adding+Hooks
import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
import org.sonar.api.server.Server;
public class MyHook implements PostProjectAnalysisTask {
private final Server server;
public MyHook(Server server) {
this.server = server;
}
#Override
public void finished(ProjectAnalysis analysis) {
QualityGate gate = analysis.getQualityGate();
if (gate.getStatus()== QualityGate.Status.ERROR) {
String baseUrl = server.getURL();
// TODO send notification
}
}

Testing Custom Gradle Plugins: Providing Configuration Data

I am trying to test a custom Gradle plugin that is configured via an Extension object that would normally be present in the build.gradle file.
For example, my build.gradle would normally look something like this:
{
apply plugin: 'foobarConfigurator'
[... stuff ...]
foobarConfig {
bar = 'boop'
baz = 'baap'
bot = 'faab'
}
[... stuff ...]
}
In my custom plugin class I have code that does this in the apply method:
def config = project.extensions.create('foobarConfig', FooBarConfig)
It's unclear to me how in a JUnit test I should write my test methods so that I can provide and test for different configuration values in a foobarConfiguration in a Project instance created by the ProjectBuilder class.
Any help appreciated, thanks!
If you intend to write a JUnit test for your extension, you can simply create an instance and configure it programmatically:
class FooBarConfigTest {
private FooBarConfig fooBarConfig
#Before
public void before() {
fooBarConfig = new FooBarConfig()
}
#Test
public void example() {
fooBarConfig.bar = 'boop'
assertEquals("expectedIfBarIsBoop", fooBarConfig.someMethod())
}
}
On the other hand, to test the plugin itself, you can use a ProjectBuilder and find the extension by type:
class MyPluginTest {
private MyPlugin plugin
private Project project
#Before
public void before() {
project = ProjectBuilder.builder().build();
plugin = new MyPlugin()
plugin.apply(project)
}
#Test
public void example() {
FooBarConfig foobarConfig = project.extensions.findByType(FooBarConfig)
assertNotNull(foobarConfig)
foobarConfig.bar = 'boop'
}
}

Resources