← Blog

Мощь Groovy: Reading URL Content

1 min readIn Ukrainian#Java#Groovy

Written in 2015. Tools and versions may have changed since.

Groovy adds some methods to the URL class which make reading data from an URL easy. For example the text property returns the complete contents of the document that is identified by the URL. Or we can use the eachLine() method to loop through each line of the document. Groovy also adds a toURL() to the String class to make a URL object.

def url = "http://www.mrhaki.com/url.html".toURL()

assert '''\
     Simple test document
     for testing URL extensions
     in Groovy.
''' == url.text

def result = []
    url.eachLine {
        if (it =~ /Groovy/) {
        result << it
    }
}
assert ['in Groovy.'] == result

url.withReader { reader ->
    assert 'Simple test document' == reader.readLine()
}

That’s all, just simple.

Related posts

12 May 2015Магия Groovy и SQL#Java #Groovy #Test фреймворк12 May 2015Достойная замена проперти файлам#Java #Groovy #Тест фреймворк8 Sept 2015Как развиваться тестировщику. Учим Groovy за 15 минут#Тест фреймворк #Java #Groovy

New writing is on Telegram

Short notes on testing and automation in the Automation Remarks channel, longer conversations in the podcast. Questions about this post: @spirogov.

Open the channel