0

I have a method with an array that contains names, I am using SWT Browser and with browser.execute() I want to fill the form.

My question: I want to use the generateName() method in the browser.execute() part instead of TEST as value.

Method

/**
 * generate name, lastname
 */
public void generateName() {
   Random generateName = new Random();
   String[] name = {"Liam", "Elias", "Kevin", "Julian", "Linus", "Levi", "Jonas", "Daniel", "Milan", "Maria", "Alexander", "Manfred", "Max", "Jens", "Dennis", "Oliver", "Jan", "Bob", "Henry"};
   String[] lastname = {"Meyer", "Müller", "Eilers", "Gerdes", "Schröder", "Janssen", "Ahlers", "Bruns", "Behrens", "Harms", "Schmidt", "Macke", "Kruse", "Becker", "Lange", "Suhr", "Schulte", "Weber", "Lampe"};

   // debug
   System.out.println(name[generateName.nextInt(19)] + " " + lastname[generateName.nextInt(19)]);
}

Button with listner

/**
 * Create contents of the window.
 */
protected void createContents() {
    shell = new Shell();
    shell.setSize(800, 600);
    shell.setText("SWT Application");

    Browser browser = new Browser(shell, SWT.NONE);
    browser.setUrl("www.pseudopage.com/SignUp");
    browser.setBounds(10, 10, 780, 352);

    Button btnNewButton = new Button(shell, SWT.BORDER);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            browser.execute("document.getElementById('txtFirstName').value=\"TEST\"");
        }
    });
    btnNewButton.setBounds(10, 374, 94, 28);
    btnNewButton.setText("Generate");

}

1 Answer 1

1

I am not sure if I understood your question.. So let's hope I have:

you could:

browser.execute("document.getElementById('txtFirstName').value=\"" + generateName() + "\"");

but generateName must return a value instead of printing it..

public String generateName() {
   Random generateName = new Random();
   String[] name = ...
   String[] lastname = ...

   return name[generateName.nextInt(19)] + " " + lastname[generateName.nextInt(19)];
}
Sign up to request clarification or add additional context in comments.

1 Comment

That worked! changed the method to String instead of void and the return of course. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.