The Prophets are chosen based on the merits and their characteristics which Allah swt is aware of, so He swt knows in advance the characteristics thus chooses based on such. And since Allah swt has the knowledge of the future, so He swt chooses them to be Prophets AS before even their creation. Decision of Allah swt and His will does not bound to time, however, the execution of the decision is time bounded, like Mohammad SAWW was prophet SAWW in the will of Allah swt always, He swt chose to send him SAWW when he was born in this world.
The Prophets AS have choice to do good or bad, they are tested on their capacities, thus Almighty swt sets a proof and reason for the creation for his choice, their tests are on higher levels, such as if you would have done what Prophet Yunus AS had done , you would not have ended in the belly of the whale, it is Yunus AS who was tested in such instance and did not pass, though he was infallible from sins, he did not commit a sin per se, but his error was serious and it required him to get into isolation and training so such does not repeat.