I'm using RandomStringGenerator from commons-text 1.2-SNAPSHOT to produce random user names and passwords for tests. Using those generated strings in Selenium with Chrome driver can cause
org.openqa.selenium.WebDriverException:
unknown error: ChromeDriver only supports characters in the BMP
(Session info: chrome=62.0.3202.62)
(Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.13.0-16-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Is there any way to produce BMP strings with existing functions already or do I have to go the obvious way to research all BMP codepoints to select randomly from them and risk to reinvent the wheel.
解决方案
RandomStringGenerator allows you to create a generator that is filtered by a specified range and a filtering function. Below, in Kotlin, is a function I use to generate a random BMP unicode string that's compatible with Selenium's chrome driver.
If you don't care if your random string includes non-printable characters, than you can remove the filteredBy.
private val randomBMPStringGenerator: RandomStringGenerator = RandomStringGenerator.Builder().withinRange(0x0000, 0xFFFF).filteredBy(CharacterPredicate {
c ->
val block: UnicodeBlock = UnicodeBlock.of(c)
!Character.isISOControl(c) && block !== UnicodeBlock.SPECIALS
}).build()
randomBMPStringGenerator.generate(wordLength)