I’ve been looking a bit at Selenium, and I’m beginning to like it, since I know some Java programming and find both Java and C# pretty straight-forward for simple things like this.
However, I’m struggling with a test that creates a new post in WordPress, from the Dashboard page:
This is the Selenium code (in C#):
(The Driver instance is, obviously, a driver class I’ve created – for starting the browser and connecting to the wordpress site.)
1: Driver.Instance.FindElement(By.Id("title)).SendKeys(title); 2: Thread.Sleep(1000); 3: 4: Instance.SwitchTo().Frame("content_ifr"); 5: Thread.Sleep(1000); 6: 7: Driver.Instance.SwitchTo().ActiveElement().SendKeys("something");
Now, what happens is that the title is easily found (by ID, so I wouldn’t expect problems there), and I can easily insert the title text (line 1).
But the inline frame for the post body is causing problems. When running the test, after the topic is filled in, the cursor changes to the body area (line 4) – as planned. However, nothing more happens. The SendKeys(“string”) method (ine 7) doesn’t seem to work there.
EDIT2: Of course, something suddenly changed. Without ANY change to the wordpress page, the “content_ifr” is now suddenly missing (?!!!!!?) The Selenium test fails with “unable to locate frame…”, and it’s also suddenly missing from the page source.
EDIT3: I also noticed something:
Driver.Instance.SwitchTo().Frame(iframe); Driver.Instance.FindElement(By.Id("tinymce")).SendKeys("message body");
It’s the SECOND line that makes the cursor switch to the mce field, not the line with the .SwitchTo(). However, I need the first line – the second line does nothing on its own. This is approaching something really stupid. I’ve been looking for a solution to this for a week – this doesn’t exactly bode well for Selenium. The Selenium user group doesn’t even want to answer when I ask them.
Also – if I skip the SendKeys() method in the second line, nothing happens. So, it seems that the two lines does ALLMOST what it should, right up to and including placing the cursor in the correct spot. But it never sends any text.
Below is some C# code that publishes a post. I think the main issues you have are due to timing issues.
I’ve done a bit of Selenium recently and I favour implicit waits: it waits for a maximum time period for the item to be available, but returns as soon as possible. So you can specify a max wait of 100 seconds, but if it finds it in 1 second, it will only wait 1 second. Much more efficient vs sleeping for an arbitrary length of time. See this post about Implicit and Explicit waits
But even with implicit waits, it may not solve all issues. When coding the sample below, I ran into the issue where the “Publish” button was disabled and re-enabled after some time. And that’s when you have to look at the code to see what it is doing as well. It’s times such as these where sleeps can help you fix the problem for a quick fix if you do not wish to debug too much: just be sure to set a large enough sleep time and be wary that it could be inconsistent in the future.
Java method to handle TinyMCE editor would look like:
A PHP version of olyv solution:
$content = 'my text'; $this->frame( 'content_ifr' ); $body = $this->byXPath( '//body' ); $body->click(); $script = 'arguments.innerHTML = "" + arguments + ""; '; $this->execute( [ 'script' => $script, 'args' => [ $body->toWebDriverObject(), $content ], ] ); $this->frame( null );