2015년 7월 29일 수요일

Spring-Batch Conditional Step Flow

Spring Batch Step Conditional Flow (Java Config)

fail
success
fail
success
StepA
StepC
StepB
StepD
<job id="job">
    <step id="stepA" parent="s1">
        <next on="*" to="stepB" />
        <next on="FAILED" to="stepC" />
    </step>
    <step id="stepB" parent="s2" next="stepC">
         <next on="*" to="stepD" />
         <next on="FAILED" to="stepC" />
    </step>
    <step id="stepC" parent="s3" />
    <step id="stepD" parent="s4" />
</job>
  • java Config

public Job job(Tasklet01 tasklet01, Tasklet02 tasklet02, Tasklet03 tasklet03, Tasklet04 tasklet04,) {
    return JobBuilderFactory.get("jobNmae")
                            .start(stepA(tasklet01))
                                .on("FAILED").to(StepC(tasklet03))
                            .from(StepA(tasklet01))
                            .next(StepB(tasklet02))
                                .on("FAILED").to(StepC(tasklet03))
                            .from(StepB(tasklet02))
                                .next(StepD(tasklet04))
                            .end()
                            .build();
  • You can control ExitSatus which is used on(“HERE”) using spring batch Context for example :
    I wrote the below code in Tasklet.
chunkContext.getStepContext().getStepExecution().setStatus(BatchStatus.FAILED);"

hope this help you!


2015년 7월 21일 화요일

[Selenium] getText() is not working


Sometimes I found that "driver.findElement(By.name("elementName"))  .getText()" returns null. Although there are texts on the webpage.

Then Try it 

driver.findElement(By.name("elementName")).getAttribute("textContent"))
In my case, It works!


2015년 7월 6일 월요일

Selenium WebDriver Page Reload or Ajax

Selenium WebDriver Page Reload or Ajax

  • ajax

see the reference :
1) Working with Ajax controls using Webdriver : http://seleniumeasy.com/selenium-tutorials/how-to-work-with-ajax-controls-using-webdriver
2) WebDriver Waits Examples :
http://seleniumeasy.com/selenium-tutorials/webdriver-wait-examples

as you can see above link, you just use WebDricverWait class

WebDriverWait wait = new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));

2) Slanec ‘s anser (there are code for page reload):
http://stackoverflow.com/questions/11001030/how-i-can-check-whether-page-is-loaded-completely-or-not-in-web-driver/11002061#11002061

WebDriverWait wait = new WebDriverWait(driver, 10);  // you can reuse this one

WebElement elem = driver.findElement(By.id("myInvisibleElement"));
elem.click();
wait.until(ExpectedConditions.visibilityOf(elem));

(Selenium WebDriver) get Value in disabled element and set Value in readonly element using javaScript

(Selenium WebDriver) get Value in disabled element and set Value in readonly element using javaScript

nomally you can get attribute data in element using below code

driver.findElement(By.id(elmentName)).getAttribute("attributeName")

but in case the element is readonly or disabled, you cannot. (at least i cannot) so I used javaScript!

you can also use javaScript when you are using Selenium like
((JavascriptExecutor)driver).executeScript(String script, Object... args)

here are my code.

  • get value in disabled element
((JavascriptExecutor) driver).executeScript("return document.getElementById('elementName').value;",""));
  • set value in readonly element
String data = "Hello World"
WebElement element = driver.findElement(By.name(elementName));
        ((JavascriptExecutor) driver).executeScript("arguments[0].value=arguments[1]", element, data);

2015년 7월 5일 일요일

(Spring boot + Spring Batch + tasklet +JobParameter) How to pass jobParameter to Tasklet

(Spring boot + Spring Batch + tasklet +JobParameter) How to pass jobParameter to Tasklet

I tried to pass jobParameter which user(anonymous) would set using tasklet(not itemReader or itemWriter, there are a lots of examples about how to pass jobParameter using itemReader & itemWriter) but I encountered below error code.

error code “no job configuration with the name was registered”

Then I found out how to fix it.

just Add the below code in your batch configuration code. The below code will register your job name. after then they can read job parameter in tasklet

@Bean
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor( JobRegistry jobRegistry) {
JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
    return jobRegistryBeanPostProcessor;
}

my whole BatchConfiguration code is like

@PropertySource("classpath:application.yml")
@AutoConfigureAfter(value = { BatchAutoConfiguration.class })
@EnableBatchProcessing
@Configuration
public class SeleniumBatchConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) {
    JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
    jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
return jobRegistryBeanPostProcessor;
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("your job name")        .start(requestForSignUpSMT3rdPartyStep()).build();
    }

    @Bean
    public Step step() {
        return stepBuilderFactory.get("your step name")
                .allowStartIfComplete(true)
                .tasklet(new RequestForSignUpSMT3rdPartyTasklet())
                .build();
    }

(sorry that my code look bad in blog)

Set jobParameter using jobParameterBuilder

It is just example. I use this jobParameter to run job using JobLauncher.

    User user = new User();
    user.setComment("This is Comment");
    user.setEmail("sooyoung32@gmail.com");
    user.setFirstName("NARASIMHAN");
    user.setLastName("NARASIMHAN");
    user.setEsiid("10443720003427889");


JobParameters parameters = new JobParametersBuilder()
                                                        .addDate("date", new Date()) //date will ba a key for jobInstance
                                                        .addString("firstName", user.getFirstName())
                                                        .addString("lastName", user.getLastName())
                                                        .addString("esiid", user.getEsiid())
                                                        .addString("comment", user.getComment())
                                                        .addString("email", user.getEmail())
                                                        .toJobParameters();

how to get JobParameter(which we set above) in Tasklek

When you make tasklet class you should implement Tasklet. Then you must implement “execute” method that have SetpContribution and ChunkContext as parameters. We use ChunkContext to get jobParameter.

String date = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("date");
        String email = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("email");
        String firstName = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("firstName");
        String lastName = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("lastName");
        String esiid = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("esiid");
        String comment = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("comment");